Repository navigation
[3.5.1] Type 'NonNullable<Partial<Config>[T]>' is not assignable to type 'Config[T]' #31675
Copy link
Copy link
Closed
Labels
Fix AvailableA PR has been opened for this issueA PR has been opened for this issueNeeds InvestigationThis issue needs a team member to investigate its status.This issue needs a team member to investigate its status.RescheduledThis issue was previously scheduled to an earlier milestoneThis issue was previously scheduled to an earlier milestone
Milestone
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:
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
Fix AvailableA PR has been opened for this issueA PR has been opened for this issueNeeds InvestigationThis issue needs a team member to investigate its status.This issue needs a team member to investigate its status.RescheduledThis issue was previously scheduled to an earlier milestoneThis issue was previously scheduled to an earlier milestone
Activity
wezleytsai commentedon Nov 11, 2019
My current workaround for this issue:
13 remaining items