Skip to content

Commit 2cc17e8

Browse files
committed
Don't widen unique symbol for different declarations
1 parent c32d87f commit 2cc17e8

7 files changed

+319
-212
lines changed

src/compiler/checker.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11422,11 +11422,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1142211422
reportErrorsFromWidening(declaration, type);
1142311423
}
1142411424

11425-
// always widen a 'unique symbol' type if the type was created for a different declaration.
11426-
if (type.flags & TypeFlags.UniqueESSymbol && (isBindingElement(declaration) || !declaration.type) && type.symbol !== getSymbolOfDeclaration(declaration)) {
11427-
type = esSymbolType;
11428-
}
11429-
1143011425
return getWidenedType(type);
1143111426
}
1143211427

tests/baselines/reference/issue55901.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export declare const l2 = "A";
3434
export declare let l3: "A";
3535
export declare let a4: string;
3636
export declare const s: unique symbol;
37-
export declare const s1: symbol;
38-
export declare const s2: symbol;
39-
export declare let s3: symbol;
40-
export declare let s4: symbol;
37+
export declare const s1: typeof s;
38+
export declare const s2: typeof s;
39+
export declare let s3: typeof s;
40+
export declare let s4: typeof s;

tests/baselines/reference/issue55901.types

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,22 @@ export const s = Symbol();
3131
>Symbol : SymbolConstructor
3232

3333
export const s1 = s as typeof s;
34-
>s1 : symbol
34+
>s1 : unique symbol
3535
>s as typeof s : unique symbol
3636
>s : unique symbol
3737
>s : unique symbol
3838

3939
export const s2 = s;
40-
>s2 : symbol
40+
>s2 : unique symbol
4141
>s : unique symbol
4242

4343
export let s3 = s as typeof s;
44-
>s3 : symbol
44+
>s3 : unique symbol
4545
>s as typeof s : unique symbol
4646
>s : unique symbol
4747
>s : unique symbol
4848

4949
export let s4 = s;
50-
>s4 : symbol
50+
>s4 : unique symbol
5151
>s : unique symbol
5252

tests/baselines/reference/uniqueSymbols.types

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const constTypeAndCall: unique symbol = Symbol();
2929

3030
// declaration from initializer
3131
const constInitToConstCall = constCall;
32-
>constInitToConstCall : symbol
32+
>constInitToConstCall : unique symbol
3333
>constCall : unique symbol
3434

3535
const constInitToLetCall = letCall;
@@ -41,11 +41,11 @@ const constInitToVarCall = varCall;
4141
>varCall : symbol
4242

4343
const constInitToConstDeclAmbient = constType;
44-
>constInitToConstDeclAmbient : symbol
44+
>constInitToConstDeclAmbient : unique symbol
4545
>constType : unique symbol
4646

4747
let letInitToConstCall = constCall;
48-
>letInitToConstCall : symbol
48+
>letInitToConstCall : unique symbol
4949
>constCall : unique symbol
5050

5151
let letInitToLetCall = letCall;
@@ -57,11 +57,11 @@ let letInitToVarCall = varCall;
5757
>varCall : symbol
5858

5959
let letInitToConstDeclAmbient = constType;
60-
>letInitToConstDeclAmbient : symbol
60+
>letInitToConstDeclAmbient : unique symbol
6161
>constType : unique symbol
6262

6363
var varInitToConstCall = constCall;
64-
>varInitToConstCall : symbol
64+
>varInitToConstCall : unique symbol
6565
>constCall : unique symbol
6666

6767
var varInitToLetCall = letCall;
@@ -73,7 +73,7 @@ var varInitToVarCall = varCall;
7373
>varCall : symbol
7474

7575
var varInitToConstDeclAmbient = constType;
76-
>varInitToConstDeclAmbient : symbol
76+
>varInitToConstDeclAmbient : unique symbol
7777
>constType : unique symbol
7878

7979
// declaration from initializer with type query
@@ -201,19 +201,19 @@ declare const c: C;
201201
>c : C
202202

203203
const constInitToCReadonlyStaticCall = C.readonlyStaticCall;
204-
>constInitToCReadonlyStaticCall : symbol
204+
>constInitToCReadonlyStaticCall : unique symbol
205205
>C.readonlyStaticCall : unique symbol
206206
>C : typeof C
207207
>readonlyStaticCall : unique symbol
208208

209209
const constInitToCReadonlyStaticType = C.readonlyStaticType;
210-
>constInitToCReadonlyStaticType : symbol
210+
>constInitToCReadonlyStaticType : unique symbol
211211
>C.readonlyStaticType : unique symbol
212212
>C : typeof C
213213
>readonlyStaticType : unique symbol
214214

215215
const constInitToCReadonlyStaticTypeAndCall = C.readonlyStaticTypeAndCall;
216-
>constInitToCReadonlyStaticTypeAndCall : symbol
216+
>constInitToCReadonlyStaticTypeAndCall : unique symbol
217217
>C.readonlyStaticTypeAndCall : unique symbol
218218
>C : typeof C
219219
>readonlyStaticTypeAndCall : unique symbol
@@ -311,7 +311,7 @@ declare const i: I;
311311
>i : I
312312

313313
const constInitToIReadonlyType = i.readonlyType;
314-
>constInitToIReadonlyType : symbol
314+
>constInitToIReadonlyType : unique symbol
315315
>i.readonlyType : unique symbol
316316
>i : I
317317
>readonlyType : unique symbol
@@ -349,13 +349,13 @@ declare const l: L;
349349
>l : L
350350

351351
const constInitToLReadonlyType = l.readonlyType;
352-
>constInitToLReadonlyType : symbol
352+
>constInitToLReadonlyType : unique symbol
353353
>l.readonlyType : unique symbol
354354
>l : L
355355
>readonlyType : unique symbol
356356

357357
const constInitToLReadonlyNestedType = l.nested.readonlyNestedType;
358-
>constInitToLReadonlyNestedType : symbol
358+
>constInitToLReadonlyNestedType : unique symbol
359359
>l.nested.readonlyNestedType : unique symbol
360360
>l.nested : { readonly readonlyNestedType: unique symbol; }
361361
>l : L
@@ -486,8 +486,8 @@ f(N["s"]);
486486

487487
// property assignments/methods
488488
const o2 = {
489-
>o2 : { a: symbol; b: symbol; c: symbol; method1(): symbol; method2(): Promise<symbol>; method3(): AsyncGenerator<symbol, void, unknown>; method4(): Generator<symbol, void, unknown>; method5(p?: symbol): symbol; }
490-
>{ a: s, b: N.s, c: N["s"], method1() { return s; }, async method2() { return s; }, async * method3() { yield s; }, * method4() { yield s; }, method5(p = s) { return p; },} : { a: symbol; b: symbol; c: symbol; method1(): symbol; method2(): Promise<symbol>; method3(): AsyncGenerator<symbol, void, unknown>; method4(): Generator<symbol, void, unknown>; method5(p?: symbol): symbol; }
489+
>o2 : { a: symbol; b: symbol; c: symbol; method1(): symbol; method2(): Promise<symbol>; method3(): AsyncGenerator<symbol, void, unknown>; method4(): Generator<symbol, void, unknown>; method5(p?: unique symbol): symbol; }
490+
>{ a: s, b: N.s, c: N["s"], method1() { return s; }, async method2() { return s; }, async * method3() { yield s; }, * method4() { yield s; }, method5(p = s) { return p; },} : { a: symbol; b: symbol; c: symbol; method1(): symbol; method2(): Promise<symbol>; method3(): AsyncGenerator<symbol, void, unknown>; method4(): Generator<symbol, void, unknown>; method5(p?: unique symbol): symbol; }
491491

492492
a: s,
493493
>a : symbol
@@ -524,10 +524,10 @@ const o2 = {
524524
>s : unique symbol
525525

526526
method5(p = s) { return p; },
527-
>method5 : (p?: symbol) => symbol
528-
>p : symbol
527+
>method5 : (p?: unique symbol) => symbol
528+
>p : unique symbol
529529
>s : unique symbol
530-
>p : symbol
530+
>p : unique symbol
531531

532532
};
533533

@@ -536,65 +536,65 @@ class C0 {
536536
>C0 : C0
537537

538538
static readonly a = s;
539-
>a : symbol
539+
>a : unique symbol
540540
>s : unique symbol
541541

542542
static readonly b = N.s;
543-
>b : symbol
543+
>b : unique symbol
544544
>N.s : unique symbol
545545
>N : typeof N
546546
>s : unique symbol
547547

548548
static readonly c = N["s"];
549-
>c : symbol
549+
>c : unique symbol
550550
>N["s"] : unique symbol
551551
>N : typeof N
552552
>"s" : "s"
553553

554554
static d = s;
555-
>d : symbol
555+
>d : unique symbol
556556
>s : unique symbol
557557

558558
static e = N.s;
559-
>e : symbol
559+
>e : unique symbol
560560
>N.s : unique symbol
561561
>N : typeof N
562562
>s : unique symbol
563563

564564
static f = N["s"];
565-
>f : symbol
565+
>f : unique symbol
566566
>N["s"] : unique symbol
567567
>N : typeof N
568568
>"s" : "s"
569569

570570
readonly a = s;
571-
>a : symbol
571+
>a : unique symbol
572572
>s : unique symbol
573573

574574
readonly b = N.s;
575-
>b : symbol
575+
>b : unique symbol
576576
>N.s : unique symbol
577577
>N : typeof N
578578
>s : unique symbol
579579

580580
readonly c = N["s"];
581-
>c : symbol
581+
>c : unique symbol
582582
>N["s"] : unique symbol
583583
>N : typeof N
584584
>"s" : "s"
585585

586586
d = s;
587-
>d : symbol
587+
>d : unique symbol
588588
>s : unique symbol
589589

590590
e = N.s;
591-
>e : symbol
591+
>e : unique symbol
592592
>N.s : unique symbol
593593
>N : typeof N
594594
>s : unique symbol
595595

596596
f = N["s"];
597-
>f : symbol
597+
>f : unique symbol
598598
>N["s"] : unique symbol
599599
>N : typeof N
600600
>"s" : "s"
@@ -618,10 +618,10 @@ class C0 {
618618
>s : unique symbol
619619

620620
method5(p = s) { return p; }
621-
>method5 : (p?: symbol) => symbol
622-
>p : symbol
621+
>method5 : (p?: unique symbol) => symbol
622+
>p : unique symbol
623623
>s : unique symbol
624-
>p : symbol
624+
>p : unique symbol
625625
}
626626

627627
// non-widening positions

0 commit comments

Comments
 (0)