Open
Description
π Search Terms
optional modifier, required fields, generic, NonNullable, strictNullChecks, homomorphic mapped types
π Version & Regression Information
- This changed between versions 5.4.5 and 5.5.2
- This changed in commit or PR e418f8d (as reported by
every-ts
)
β― Playground Link
π» Code
const example = <T extends string>(): any => {
// Replacing with any specific sub-type of `string` makes the types correct again.
// type T = string;
type SomeOptional = { [key in T]?: 1 };
type AllRequired = { [key in keyof SomeOptional]-?: 1 };
type RequiredValues = AllRequired[keyof AllRequired];
// Complains in 5.4.x, but fine in 5.5.x
const x: RequiredValues = undefined
return x;
};
π Actual behavior
Using -?
leaves the fields marked as optional, and taking the values of the resulting type gives a union with undefined
.
π Expected behavior
Using -?
makes all fields required, and taking the values of the resulting type gives a union of field types.
Additional information about the issue
Requires a --strictNullChecks
flag.
Replacing keyof SomeOptional
with NonNullable<keyof SomeOptional>
or keyof SomeOptional as keyof SomeOptional
fixes the issue. It looks like this problem is limited to homomorphic mapped types.
Using Required<{ [P in keyof SomeOptional]: 1; }>
in place of { [key in keyof SomeOptional]-?: 1 }
results in the same behaviour.