Skip to content

Commit 3b73ce4

Browse files
authored
Merge pull request #21585 from Microsoft/fix21584
Fix initializer assignability for unique symbol
2 parents cde1cd0 + 6f792f5 commit 3b73ce4

File tree

7 files changed

+29
-9
lines changed

7 files changed

+29
-9
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8418,7 +8418,7 @@ namespace ts {
84188418
if (isValidESSymbolDeclaration(node)) {
84198419
const symbol = getSymbolOfNode(node);
84208420
const links = getSymbolLinks(symbol);
8421-
return links.type || (links.type = createUniqueESSymbolType(symbol));
8421+
return links.uniqueESSymbolType || (links.uniqueESSymbolType = createUniqueESSymbolType(symbol));
84228422
}
84238423
return esSymbolType;
84248424
}

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3311,6 +3311,7 @@ namespace ts {
33113311
immediateTarget?: Symbol; // Immediate target of an alias. May be another alias. Do not access directly, use `checker.getImmediateAliasedSymbol` instead.
33123312
target?: Symbol; // Resolved (non-alias) target of an alias
33133313
type?: Type; // Type of value symbol
3314+
uniqueESSymbolType?: Type; // UniqueESSymbol type for a symbol
33143315
declaredType?: Type; // Type of class, interface, enum, type alias, or type parameter
33153316
typeParameters?: TypeParameter[]; // Type parameters of type alias (undefined if non-generic)
33163317
outerTypeParameters?: TypeParameter[]; // Outer type parameters of anonymous object type

tests/baselines/reference/uniqueSymbolsErrors.errors.txt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@ tests/cases/conformance/types/uniqueSymbol/uniqueSymbolsErrors.ts(82,29): error
5858
tests/cases/conformance/types/uniqueSymbol/uniqueSymbolsErrors.ts(82,45): error TS1335: 'unique symbol' types are not allowed here.
5959
tests/cases/conformance/types/uniqueSymbol/uniqueSymbolsErrors.ts(83,36): error TS1335: 'unique symbol' types are not allowed here.
6060
tests/cases/conformance/types/uniqueSymbol/uniqueSymbolsErrors.ts(83,52): error TS1335: 'unique symbol' types are not allowed here.
61+
tests/cases/conformance/types/uniqueSymbol/uniqueSymbolsErrors.ts(87,7): error TS2322: Type 'unique symbol' is not assignable to type 'string'.
6162

6263

63-
==== tests/cases/conformance/types/uniqueSymbol/uniqueSymbolsErrors.ts (60 errors) ====
64+
==== tests/cases/conformance/types/uniqueSymbol/uniqueSymbolsErrors.ts (61 errors) ====
6465
// declarations
6566
declare const invalidUniqueType: unique number;
6667
~~~~~~
@@ -265,5 +266,8 @@ tests/cases/conformance/types/uniqueSymbol/uniqueSymbolsErrors.ts(83,52): error
265266
~~~~~~~~~~~~~
266267
!!! error TS1335: 'unique symbol' types are not allowed here.
267268

268-
269-
269+
// initializer assignability
270+
// https://github.com/Microsoft/TypeScript/issues/21584
271+
const shouldNotBeAssignable: string = Symbol();
272+
~~~~~~~~~~~~~~~~~~~~~
273+
!!! error TS2322: Type 'unique symbol' is not assignable to type 'string'.

tests/baselines/reference/uniqueSymbolsErrors.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,9 @@ declare const invalidMappedType: { [P in string]: unique symbol };
8383
declare const invalidUnion: unique symbol | unique symbol;
8484
declare const invalidIntersection: unique symbol | unique symbol;
8585

86-
87-
86+
// initializer assignability
87+
// https://github.com/Microsoft/TypeScript/issues/21584
88+
const shouldNotBeAssignable: string = Symbol();
8889

8990
//// [uniqueSymbolsErrors.js]
9091
// classes
@@ -109,3 +110,6 @@ class InvalidClass {
109110
static get invalidStaticGetter() { return; }
110111
static set invalidStaticSetter(arg) { return; }
111112
}
113+
// initializer assignability
114+
// https://github.com/Microsoft/TypeScript/issues/21584
115+
const shouldNotBeAssignable = Symbol();

tests/baselines/reference/uniqueSymbolsErrors.symbols

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,5 +234,9 @@ declare const invalidUnion: unique symbol | unique symbol;
234234
declare const invalidIntersection: unique symbol | unique symbol;
235235
>invalidIntersection : Symbol(invalidIntersection, Decl(uniqueSymbolsErrors.ts, 82, 13))
236236

237-
237+
// initializer assignability
238+
// https://github.com/Microsoft/TypeScript/issues/21584
239+
const shouldNotBeAssignable: string = Symbol();
240+
>shouldNotBeAssignable : Symbol(shouldNotBeAssignable, Decl(uniqueSymbolsErrors.ts, 86, 5))
241+
>Symbol : Symbol(Symbol, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --))
238242

tests/baselines/reference/uniqueSymbolsErrors.types

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,5 +234,10 @@ declare const invalidUnion: unique symbol | unique symbol;
234234
declare const invalidIntersection: unique symbol | unique symbol;
235235
>invalidIntersection : symbol
236236

237-
237+
// initializer assignability
238+
// https://github.com/Microsoft/TypeScript/issues/21584
239+
const shouldNotBeAssignable: string = Symbol();
240+
>shouldNotBeAssignable : string
241+
>Symbol() : unique symbol
242+
>Symbol : SymbolConstructor
238243

tests/cases/conformance/types/uniqueSymbol/uniqueSymbolsErrors.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,6 @@ declare const invalidMappedType: { [P in string]: unique symbol };
8484
declare const invalidUnion: unique symbol | unique symbol;
8585
declare const invalidIntersection: unique symbol | unique symbol;
8686

87-
87+
// initializer assignability
88+
// https://github.com/Microsoft/TypeScript/issues/21584
89+
const shouldNotBeAssignable: string = Symbol();

0 commit comments

Comments
 (0)