Closed
Description
TypeScript Version: 3.5.1, 3.6.0-dev.20190530
Search Terms:
partial nonnullable generic mapped strictNullChecks
Code
interface Config {
string: string
// There need to be different types in here for the error to occur
number: number
}
function getConfigOrDefault<T extends keyof Config>(
userConfig: Partial<Config>,
key: T,
defaultValue: Config[T]
): Config[T] {
const userValue = userConfig[key] // Partial<Config>[T]
const explicitAssertion = userConfig[key] as Config[T] | undefined // allowed
// Partial<Config>[T] - already incorrect in 3.4.5?
const simpleCheck = userValue ? userValue : defaultValue
// 3.4.5: Config[T]
// 3.5.1, 3.6.0-dev.20190530: Config[T] | NonNullable<Partial<Config>[T]>
const assertedCheck = userValue ? userValue! : defaultValue
return assertedCheck
}
Expected behavior:
No error
Actual behavior:
Type 'Config[T] | NonNullable<Partial<Config>[T]>' is not assignable to type 'Config[T]'.
Type 'NonNullable<Partial<Config>[T]>' is not assignable to type 'Config[T]'.
Type 'Partial<Config>[T]' is not assignable to type 'Config[T]'.
Type 'Partial<Config>' is not assignable to type 'Config'.
Types of property 'string' are incompatible.
Type 'string | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'.
Type 'Partial<Config>[T]' is not assignable to type 'string & number'.
Type 'Partial<Config>[T]' is not assignable to type 'string'.
Type 'string | number' is not assignable to type 'Config[T]'.
Type 'string' is not assignable to type 'Config[T]'.
Type 'string' is not assignable to type 'string & number'.
Type 'string' is not assignable to type 'number'.
Type 'NonNullable<Partial<Config>[T]>' is not assignable to type 'string & number'.
Type 'Partial<Config>[T]' is not assignable to type 'string & number'.
Type 'string | number | undefined' is not assignable to type 'string & number'.
Type 'undefined' is not assignable to type 'string & number'.
Type 'undefined' is not assignable to type 'string'.
Type 'Partial<Config>[T]' is not assignable to type 'string'.
Type 'string | number' is not assignable to type 'string & number'.
Type 'string' is not assignable to type 'string & number'.
Type 'string' is not assignable to type 'number'.
Type 'NonNullable<Partial<Config>[T]>' is not assignable to type 'string'.
Type 'Partial<Config>[T]' is not assignable to type 'string'.
Type 'string | number | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'.
Type 'string | number' is not assignable to type 'string'.
Type 'number' is not assignable to type 'string'.ts(2322)
with strictNullChecks: true
Playground Link:
(error does not occur there - which TS version does the playground run on?)
Related Issues:
These issues looked similar but were already reported for earlier versions of TypeScript: