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
typeListItem={id: stringlabel: stringdisabled?: boolean}constlistA=[{id: 'foo',label: 'Foo',// disabled: true}]asconstsatisfiesreadonlyListItem[]// "Property 'disabled' does not exist on type"// Should not error here, since the listA should satisfy ListItem type which has disabled property// EXPECTED: listA[0].disabled has type boolean | undefinedlistA[0].disabled// ^?constlistB=[{id: 'foo',label: 'Foo',disabled: true}]asconstsatisfiesreadonlyListItem[]// Here the type is correctly resolved to `true`listB[0].disabled// ^?
🙁 Actual behavior
Readonly literal types that satisfy a type don't carry over optional properties that might not be set. Setting the type directly would mean we lose the literal types.
🙂 Expected behavior
Optional properties are carried over when a const satisfies some type. Expected type of listA is { id: 'foo', label: 'Foo', disabled?: boolean }[]
The text was updated successfully, but these errors were encountered:
Huh, I recall that the primary debate over satisfies was basically between “type-check only” vs. “safe upcast” (with the former winning out); it never even struck me there was this third possibility of “infer ExpressionType & CheckType”; that’d have been kind of clever, actually.
To OP: Note that explicitly setting to undefined won’t work anymore if you ever decide to turn on exactOptionalPropertyTypes.
It would be nice to have some way to define that a constant variable conforms to some type and would carry over the type. @RyanCavanaugh Maybe could be a new feature request? or what are your thoughts on this topic?
Bug Report
🔎 Search Terms
satisfies, optional property
🕗 Version & Regression Information
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
Readonly literal types that satisfy a type don't carry over optional properties that might not be set. Setting the type directly would mean we lose the literal types.
🙂 Expected behavior
Optional properties are carried over when a const satisfies some type. Expected type of
listA
is{ id: 'foo', label: 'Foo', disabled?: boolean }[]
The text was updated successfully, but these errors were encountered: