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
consta=Symbol();constb=Symbol();functionidentity<T>(t : T) : T{returnt;}/** * Mouseover `identity(a)`, * Tooltip shows, * * ``` * function identity<typeof a>(t: typeof a): typeof a * ``` *//** * Expected : typeof a * * Actual : symbol */constactual=identity(a);/** * Error * * Type 'symbol' is not assignable to type 'unique symbol'. */constexpectA : typeofa=actual;/////////////////////////////////////////////////functiongiveTypeOfA() : typeofa{returna;}/** * Expected : typeof a * * Actual : symbol */constactual2=giveTypeOfA();/** * Error * * Type 'symbol' is not assignable to type 'unique symbol'. */constexpectA2 : typeofa=actual2;
I know this ticket is closed and marked as "Working as Intended", but IMO the current behavior makes it a bit harder than necessary to create custom Unit types that are guaranteed to not clash with string/number literals and "survive" being passed through generic function calls without getting widened to just symbol.
I'm using AnyhowStep's tuple "boxing" workaround for now (thanks for pointing that out!) but I would like to ask if y'all could reconsider, and change the unique symbol widening behavior to be the same as the one used for string/number literals.
I'm not sure if doing so could have additional implications to the type system... (Maybe something breaks because of it?) In that case, perhaps we could maybe opt out of the widening behavior via the unique type modifier?
It's confusing to allow this because it's unclear if : unique T means "Returns the same unique symbol each time" or "Returns a new unique symbol each time".
Activity
AnyhowStep commentedon Jul 3, 2019
The current workaround is,
Playground
Returning a tuple with one element also works,
Playground
However, this does not work,
Playground
So, it seems like I have to "box" the
unique symboluntil I actually need it.RyanCavanaugh commentedon Jul 26, 2019
The uniqueness of a symbol goes away through widening.
AnyhowStep commentedon Jul 26, 2019
Why would it widen for
foo () : typeof abut notfoo() : [typeof a]?typescript-bot commentedon Jul 28, 2019
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.
artalar commentedon Aug 15, 2019
Sorry, but Im really "Cannot return unique symbol":
coreh commentedon Feb 20, 2020
I know this ticket is closed and marked as "Working as Intended", but IMO the current behavior makes it a bit harder than necessary to create custom Unit types that are guaranteed to not clash with
string/numberliterals and "survive" being passed through generic function calls without getting widened to justsymbol.I'm using AnyhowStep's tuple "boxing" workaround for now (thanks for pointing that out!) but I would like to ask if y'all could reconsider, and change the
unique symbolwidening behavior to be the same as the one used forstring/numberliterals.I'm not sure if doing so could have additional implications to the type system... (Maybe something breaks because of it?) In that case, perhaps we could maybe opt out of the widening behavior via the
uniquetype modifier?RyanCavanaugh commentedon Feb 20, 2020
It's confusing to allow this because it's unclear if
: unique Tmeans "Returns the same unique symbol each time" or "Returns a new unique symbol each time".