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
Search Terms:
generic, conditional type, cannot be assigned, generic return value
Expected behavior:
A value of type X is a valid return value for a function returning Always<X>, since the latter is a generic conditional type that always evaluates to X.
Actual behavior:
Does not compile. Error message: Type 'X' is not assignable to type 'Always<X>'. (2322)
Related Issues: #31751 seems to be more about never type semantics.
Code
You can substitute any type for any in the definition below and it will fail regardless.
exporttypeAlways<X>=Xextendsany ? X : Xconstf=<X>(): Always<X>=>/*error*/nullasunknownasX/*error*/
The text was updated successfully, but these errors were encountered:
eibens
changed the title
Generic conditional type X extends any ? X : X does not allow generic assignment of X?
Generic conditional type X extends any ? X : X does not allow generic assignment of X
Mar 7, 2020
#37279 is actually extremely similar and describes how I encountered this problem in the first place. I guess since X may also be a type union it is working as intended. Same solution applies:
exporttypeAlways<X>=[X]extends[any] ? X : Xconstf=<X>(): Always<X>=>nullasunknownasX
TypeScript Version: Nightly
Search Terms:
generic, conditional type, cannot be assigned, generic return value
Expected behavior:
A value of type
X
is a valid return value for a function returningAlways<X>
, since the latter is a generic conditional type that always evaluates toX
.Actual behavior:
Does not compile. Error message:
Type 'X' is not assignable to type 'Always<X>'. (2322)
Related Issues:
#31751 seems to be more about
never
type semantics.Code
You can substitute any type for
any
in the definition below and it will fail regardless.Output
Compiler Options
Playground Link: Provided
The text was updated successfully, but these errors were encountered: