Search Terms
Symbol, unique, generated, function call
Suggestion
With two functions returning unique symbol.
declare function randomSymbol(): { readonly id: unique symbol };
declare function randomSymbol2(): { readonly id: unique symbol };
When comparing the symbol types when calling these two functions, we get a (correct) error from the compiler telling us that these types are incompatible.
const a = randomSymbol();
const b = randomSymbol2();
a.id === a.id;
a.id === b.id; // TS ERROR: Expected behaviour. 👍
If we call the same function two times though, the types are the same.
const a = randomSymbol();
const b = randomSymbol();
a.id === a.id;
a.id === b.id; // NO ERROR: But I would expect these two types to be different too 👎
I understand that this can be expected in some cases, though there is currently no way to tell the compiler to generate a different unique symbol for each function call.
Use Cases
My current use-case is for creating two objects using the same constructor, that are structurally identical, but that would not be compatible between them (kind of opaque types).
Maybe this should be through the usage of a new keyword to prevent compatibility issues with existing codebases.
Examples
https://www.typescriptlang.org/play?#code/CYUwxgNghgTiAEAzArgOzAFwJYHtXxilWBwFsBlAT1ICMcIAKASgC54BvAkKE1CS+FmBs0WAI7IEAZ2p0I8AL4BuAFChIsBCnTY8BIiQqz6AJmZtOcHnn6Dh8UROnH5ylSvYr48MHikZ4KHgAXn1iMipaemZVb19Uf3gaELDDSLkzJlUvQIA6IRDg0Kh84Fi8gqLQmlKleAB6evgAeQBpFQV3Tzi-AKDQwnCjKMYsnPjE5IGDCJcY928SyqKKspyl4ELq2oamgEl4AHccZAhNkAAPAAdwAIwACxApBAxj+AxKG6l3nCSEYCwiEQIDgqDuOBwHRUQA
Checklist
My suggestion meets these guidelines:
Search Terms
Symbol, unique, generated, function call
Suggestion
With two functions returning
unique symbol.When comparing the symbol types when calling these two functions, we get a (correct) error from the compiler telling us that these types are incompatible.
If we call the same function two times though, the types are the same.
I understand that this can be expected in some cases, though there is currently no way to tell the compiler to generate a different unique symbol for each function call.
Use Cases
My current use-case is for creating two objects using the same constructor, that are structurally identical, but that would not be compatible between them (kind of opaque types).
Maybe this should be through the usage of a new keyword to prevent compatibility issues with existing codebases.
Examples
https://www.typescriptlang.org/play?#code/CYUwxgNghgTiAEAzArgOzAFwJYHtXxilWBwFsBlAT1ICMcIAKASgC54BvAkKE1CS+FmBs0WAI7IEAZ2p0I8AL4BuAFChIsBCnTY8BIiQqz6AJmZtOcHnn6Dh8UROnH5ylSvYr48MHikZ4KHgAXn1iMipaemZVb19Uf3gaELDDSLkzJlUvQIA6IRDg0Kh84Fi8gqLQmlKleAB6evgAeQBpFQV3Tzi-AKDQwnCjKMYsnPjE5IGDCJcY928SyqKKspyl4ELq2oamgEl4AHccZAhNkAAPAAdwAIwACxApBAxj+AxKG6l3nCSEYCwiEQIDgqDuOBwHRUQA
Checklist
My suggestion meets these guidelines: