You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constexample=<Textendsstring>(): any=>{// Replacing with any specific sub-type of `string` makes the types correct again.// type T = string;typeSomeOptional={[keyinT]?: 1};typeAllRequired={[keyinkeyofSomeOptional]-?: 1};typeRequiredValues=AllRequired[keyofAllRequired];// Complains in 5.4.x, but fine in 5.5.xconstx: RequiredValues=undefinedreturnx;};
🙁 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.
The text was updated successfully, but these errors were encountered:
🔎 Search Terms
optional modifier, required fields, generic, NonNullable, strictNullChecks, homomorphic mapped types
🕗 Version & Regression Information
every-ts
)⏯ Playground Link
https://www.typescriptlang.org/play/?ts=5.5.4#code/MYewdgzgLgBApgDwIYFsAOAbOMC8MA8AKvAlHGACYQzQBOAlmAOYB8AFAJQBcMSYAnrhYwA3gCgYMAPRSYAJTiYkwRkxgB3elAAWvATTRwVAM3rAaAVwBGAWij9DMEMZgADOqtcwUSANZxqHWx7Q2pQWlojWCQmJEYAOglpWRDsYjwPZgBuMSTUmABlEBQ4AHk0KHpwJAxcURgAbX9BRhhCAF0Afh4ARhgAXxzJPIdsAEEMDAUARwt6SIo6kUbmmFbm50LisoqqsBr2m26YPsHcyXyZuYWANRqLALqJqbhZ+bgKJrh+Teer94o7RySRkMAAwsUlIxqK0AKzxAAs8QQABoYFYLLBTGBsHD4vCEElQJBYAgeP9bvdHngLJQ4NiPucYJEoBZaGAYAgcoMgA
💻 Code
🙁 Actual behavior
Using
-?
leaves the fields marked as optional, and taking the values of the resulting type gives a union withundefined
.🙂 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
withNonNullable<keyof SomeOptional>
orkeyof 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.The text was updated successfully, but these errors were encountered: