-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Cannot return unique symbol #32242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
The current workaround is, const a = Symbol();
function giveTypeOfA () : { workaround : typeof a } {
return { workaround : a };
}
/**
* Expected : { workaround : typeof a }
*
* Actual : { workaround : typeof a }
*/
const actual2 = giveTypeOfA();
/**
* Assignment OK!
*/
const expectA2 : typeof a = actual2.workaround; Returning a tuple with one element also works, const a = Symbol();
function giveTypeOfA () : [typeof a] {
return [a];
}
/**
* Expected : [typeof a]
*
* Actual : [typeof a]
*/
const actual2 = giveTypeOfA();
/**
* Assignment OK!
*/
const expectA2 : typeof a = actual2[0]; However, this does not work, const a = Symbol();
function giveTypeOfA () : [typeof a] {
return [a];
}
/**
* Expected : typeof a
*
* Actual : symbol
*/
const actual2 = giveTypeOfA()[0];
/**
* Assignment Not allowed
*/
const expectA2 : typeof a = actual2; So, it seems like I have to "box" the |
The uniqueness of a symbol goes away through widening. |
Why would it widen for |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Sorry, but Im really "Cannot return unique symbol": function getSymbol() {
const uniqueSymbol: unique symbol = Symbol()
return uniqueSymbol
}
var a = getSymbol()
// no error??
var b: typeof a = getSymbol() |
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 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 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 function identity<T> (t : T) : unique T {
return t;
} |
It's confusing to allow this because it's unclear if |
TypeScript Version: 3.5.1
Search Terms:
function, return type, unique symbol
Code
Expected behavior:
actual
andactual2
should betypeof a
Actual behavior:
actual
andactual2
aresymbol
Playground Link: Playground
Related Issues:
The very last comment of #24506 asks a question about intended behaviour but it goes unanswered.
The text was updated successfully, but these errors were encountered: