Skip to content

Commit 7507b05

Browse files
authored
Don't widen unique symbols annotated by effective type nodes in JS files (microsoft#61180)
1 parent 2c3be44 commit 7507b05

File tree

6 files changed

+124
-1
lines changed

6 files changed

+124
-1
lines changed

src/compiler/checker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11821,7 +11821,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1182111821
}
1182211822

1182311823
// always widen a 'unique symbol' type if the type was created for a different declaration.
11824-
if (type.flags & TypeFlags.UniqueESSymbol && (isBindingElement(declaration) || !declaration.type) && type.symbol !== getSymbolOfDeclaration(declaration)) {
11824+
if (type.flags & TypeFlags.UniqueESSymbol && (isBindingElement(declaration) || !tryGetTypeFromEffectiveTypeNode(declaration)) && type.symbol !== getSymbolOfDeclaration(declaration)) {
1182511825
type = esSymbolType;
1182611826
}
1182711827

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/index.js(9,1): error TS2367: This comparison appears to be unintentional because the types 'typeof x' and 'typeof y' have no overlap.
2+
3+
4+
==== /index.js (1 errors) ====
5+
/** @type {unique symbol} */
6+
const x = Symbol()
7+
/** @type {unique symbol} */
8+
const y = Symbol()
9+
10+
/** @type {typeof x} */
11+
let z = x
12+
13+
z == y // error
14+
~~~~~~
15+
!!! error TS2367: This comparison appears to be unintentional because the types 'typeof x' and 'typeof y' have no overlap.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//// [tests/cases/compiler/uniqueSymbolJs2.ts] ////
2+
3+
//// [index.js]
4+
/** @type {unique symbol} */
5+
const x = Symbol()
6+
/** @type {unique symbol} */
7+
const y = Symbol()
8+
9+
/** @type {typeof x} */
10+
let z = x
11+
12+
z == y // error
13+
14+
//// [index.js]
15+
"use strict";
16+
/** @type {unique symbol} */
17+
var x = Symbol();
18+
/** @type {unique symbol} */
19+
var y = Symbol();
20+
/** @type {typeof x} */
21+
var z = x;
22+
z == y; // error
23+
24+
25+
//// [index.d.ts]
26+
/** @type {unique symbol} */
27+
declare const x: unique symbol;
28+
/** @type {unique symbol} */
29+
declare const y: unique symbol;
30+
/** @type {typeof x} */
31+
declare let z: typeof x;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//// [tests/cases/compiler/uniqueSymbolJs2.ts] ////
2+
3+
=== /index.js ===
4+
/** @type {unique symbol} */
5+
const x = Symbol()
6+
>x : Symbol(x, Decl(index.js, 1, 5))
7+
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2019.symbol.d.ts, --, --))
8+
9+
/** @type {unique symbol} */
10+
const y = Symbol()
11+
>y : Symbol(y, Decl(index.js, 3, 5))
12+
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2019.symbol.d.ts, --, --))
13+
14+
/** @type {typeof x} */
15+
let z = x
16+
>z : Symbol(z, Decl(index.js, 6, 3))
17+
>x : Symbol(x, Decl(index.js, 1, 5))
18+
19+
z == y // error
20+
>z : Symbol(z, Decl(index.js, 6, 3))
21+
>y : Symbol(y, Decl(index.js, 3, 5))
22+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//// [tests/cases/compiler/uniqueSymbolJs2.ts] ////
2+
3+
=== /index.js ===
4+
/** @type {unique symbol} */
5+
const x = Symbol()
6+
>x : unique symbol
7+
> : ^^^^^^^^^^^^^
8+
>Symbol() : unique symbol
9+
> : ^^^^^^^^^^^^^
10+
>Symbol : SymbolConstructor
11+
> : ^^^^^^^^^^^^^^^^^
12+
13+
/** @type {unique symbol} */
14+
const y = Symbol()
15+
>y : unique symbol
16+
> : ^^^^^^^^^^^^^
17+
>Symbol() : unique symbol
18+
> : ^^^^^^^^^^^^^
19+
>Symbol : SymbolConstructor
20+
> : ^^^^^^^^^^^^^^^^^
21+
22+
/** @type {typeof x} */
23+
let z = x
24+
>z : unique symbol
25+
> : ^^^^^^^^^^^^^
26+
>x : unique symbol
27+
> : ^^^^^^^^^^^^^
28+
29+
z == y // error
30+
>z == y : boolean
31+
> : ^^^^^^^
32+
>z : unique symbol
33+
> : ^^^^^^^^^^^^^
34+
>y : unique symbol
35+
> : ^^^^^^^^^^^^^
36+
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// @strict: true
2+
// @lib: esnext
3+
// @allowJS: true
4+
// @checkJs: true
5+
// @declaration: true
6+
// @outDir: dist
7+
8+
// https://github.com/microsoft/TypeScript/issues/61170
9+
10+
// @filename: /index.js
11+
/** @type {unique symbol} */
12+
const x = Symbol()
13+
/** @type {unique symbol} */
14+
const y = Symbol()
15+
16+
/** @type {typeof x} */
17+
let z = x
18+
19+
z == y // error

0 commit comments

Comments
 (0)