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:
Type 'symbol' is not assignable to type 'unique symbol'
Code
constSPECIAL: unique symbol=Symbol("sym");abstractclassBase{abstractfff(): typeofSPECIAL|number;}classDerive1extendsBase{fff(){return10;}}classDerive2extendsBase{fff(){// Error: Type 'symbol' is not assignable to type 'number | unique symbol'returnSPECIAL;}}
Expected behavior:
It works.
Actual behavior:
Type 'symbol' is not assignable to type 'number | unique symbol'
Related Issues: #24506 - but it's closed due to houcekeeping, and the code there is more complicated (probably a different usecase?),
Motivation:
I want to express the notion of some "special" values returned from a method. Like in most of the cases the method returns some value, but sometimes it returns SPECIAL constant (there can be multiple) to signal about something. Looks like there is no way to do it currently in TS using symbols (if SPECIAL is a constant string, it's doable, but still I need to cast it to as const every time I return it).
The text was updated successfully, but these errors were encountered:
This is sorta a bug, sorta a design limitation. Methods don't actually inherit their unannotated signature from their base type right now, so fff in Derive2 is actually a method with no context - so the SPECIALunique symbol type is widened to symbol to make the signature per our literal type rules. However this is then compared against the base which expects the literal unique symbol. If the literal context were projected from the base, this would not happen, so that's why this is maybe a bug, since that's expected to many people, however currently contextual types not applying to subclass methods is the designed behavior, which is why this could also be read as "by design".
In any case, as a workaround, just annotate the return type on fff explicitly (: typeof SPECIAL) and it should work out.
TypeScript Version:
3.4.0-dev.201xxxxx
Search Terms:
Type 'symbol' is not assignable to type 'unique symbol'
Code
Expected behavior:
It works.
Actual behavior:
Type 'symbol' is not assignable to type 'number | unique symbol'
Playground Link:
playground link
Related Issues:
#24506 - but it's closed due to houcekeeping, and the code there is more complicated (probably a different usecase?),
Motivation:
I want to express the notion of some "special" values returned from a method. Like in most of the cases the method returns some value, but sometimes it returns
SPECIAL
constant (there can be multiple) to signal about something. Looks like there is no way to do it currently in TS using symbols (ifSPECIAL
is a constant string, it's doable, but still I need to cast it toas const
every time I return it).The text was updated successfully, but these errors were encountered: