Skip to content

Commit 9586288

Browse files
authored
Filter symbol property names out of index signature assignability checks (#22398)
1 parent 55bffba commit 9586288

6 files changed

+124
-2
lines changed

src/compiler/checker.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8008,10 +8008,10 @@ namespace ts {
80088008
}
80098009

80108010
function getLiteralTypeFromPropertyName(prop: Symbol) {
8011-
const links = getSymbolLinks(prop);
8011+
const links = getSymbolLinks(getLateBoundSymbol(prop));
80128012
if (!links.nameType) {
80138013
if (links.target) {
8014-
Debug.assert(links.target.escapedName === prop.escapedName, "Target symbol and symbol do not have the same name");
8014+
Debug.assert(links.target.escapedName === prop.escapedName || links.target.escapedName === InternalSymbolName.Computed, "Target symbol and symbol do not have the same name");
80158015
links.nameType = getLiteralTypeFromPropertyName(links.target);
80168016
}
80178017
else {
@@ -10558,6 +10558,11 @@ namespace ts {
1055810558
if (isIgnoredJsxProperty(source, prop, /*targetMemberType*/ undefined)) {
1055910559
continue;
1056010560
}
10561+
// Skip over symbol-named members
10562+
const nameType = getLiteralTypeFromPropertyName(prop);
10563+
if (nameType !== undefined && !(isRelatedTo(nameType, stringType) || isRelatedTo(nameType, numberType))) {
10564+
continue;
10565+
}
1056110566
if (kind === IndexKind.String || isNumericLiteralName(prop.escapedName)) {
1056210567
const related = isRelatedTo(getTypeOfSymbol(prop), target, reportErrors);
1056310568
if (!related) {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
tests/cases/compiler/uniqueSymbolAllowsIndexInObjectWithIndexSignature.ts(10,5): error TS2322: Type '{ [SYM]: "str"; }' is not assignable to type 'I'.
2+
Types of property '[SYM]' are incompatible.
3+
Type '"str"' is not assignable to type '"sym"'.
4+
5+
6+
==== tests/cases/compiler/uniqueSymbolAllowsIndexInObjectWithIndexSignature.ts (1 errors) ====
7+
// https://github.com/Microsoft/TypeScript/issues/21962
8+
export const SYM = Symbol('a unique symbol');
9+
10+
export interface I {
11+
[SYM]: 'sym';
12+
[x: string]: 'str';
13+
}
14+
15+
let a: I = {[SYM]: 'sym'}; // Expect ok
16+
let b: I = {[SYM]: 'str'}; // Expect error
17+
~
18+
!!! error TS2322: Type '{ [SYM]: "str"; }' is not assignable to type 'I'.
19+
!!! error TS2322: Types of property '[SYM]' are incompatible.
20+
!!! error TS2322: Type '"str"' is not assignable to type '"sym"'.
21+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//// [uniqueSymbolAllowsIndexInObjectWithIndexSignature.ts]
2+
// https://github.com/Microsoft/TypeScript/issues/21962
3+
export const SYM = Symbol('a unique symbol');
4+
5+
export interface I {
6+
[SYM]: 'sym';
7+
[x: string]: 'str';
8+
}
9+
10+
let a: I = {[SYM]: 'sym'}; // Expect ok
11+
let b: I = {[SYM]: 'str'}; // Expect error
12+
13+
14+
//// [uniqueSymbolAllowsIndexInObjectWithIndexSignature.js]
15+
"use strict";
16+
exports.__esModule = true;
17+
// https://github.com/Microsoft/TypeScript/issues/21962
18+
exports.SYM = Symbol('a unique symbol');
19+
var a = (_a = {}, _a[exports.SYM] = 'sym', _a); // Expect ok
20+
var b = (_b = {}, _b[exports.SYM] = 'str', _b); // Expect error
21+
var _a, _b;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
=== tests/cases/compiler/uniqueSymbolAllowsIndexInObjectWithIndexSignature.ts ===
2+
// https://github.com/Microsoft/TypeScript/issues/21962
3+
export const SYM = Symbol('a unique symbol');
4+
>SYM : Symbol(SYM, Decl(uniqueSymbolAllowsIndexInObjectWithIndexSignature.ts, 1, 12))
5+
>Symbol : Symbol(Symbol, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --))
6+
7+
export interface I {
8+
>I : Symbol(I, Decl(uniqueSymbolAllowsIndexInObjectWithIndexSignature.ts, 1, 45))
9+
10+
[SYM]: 'sym';
11+
>[SYM] : Symbol(I[SYM], Decl(uniqueSymbolAllowsIndexInObjectWithIndexSignature.ts, 3, 20))
12+
>SYM : Symbol(SYM, Decl(uniqueSymbolAllowsIndexInObjectWithIndexSignature.ts, 1, 12))
13+
14+
[x: string]: 'str';
15+
>x : Symbol(x, Decl(uniqueSymbolAllowsIndexInObjectWithIndexSignature.ts, 5, 3))
16+
}
17+
18+
let a: I = {[SYM]: 'sym'}; // Expect ok
19+
>a : Symbol(a, Decl(uniqueSymbolAllowsIndexInObjectWithIndexSignature.ts, 8, 3))
20+
>I : Symbol(I, Decl(uniqueSymbolAllowsIndexInObjectWithIndexSignature.ts, 1, 45))
21+
>[SYM] : Symbol([SYM], Decl(uniqueSymbolAllowsIndexInObjectWithIndexSignature.ts, 8, 12))
22+
>SYM : Symbol(SYM, Decl(uniqueSymbolAllowsIndexInObjectWithIndexSignature.ts, 1, 12))
23+
24+
let b: I = {[SYM]: 'str'}; // Expect error
25+
>b : Symbol(b, Decl(uniqueSymbolAllowsIndexInObjectWithIndexSignature.ts, 9, 3))
26+
>I : Symbol(I, Decl(uniqueSymbolAllowsIndexInObjectWithIndexSignature.ts, 1, 45))
27+
>[SYM] : Symbol([SYM], Decl(uniqueSymbolAllowsIndexInObjectWithIndexSignature.ts, 9, 12))
28+
>SYM : Symbol(SYM, Decl(uniqueSymbolAllowsIndexInObjectWithIndexSignature.ts, 1, 12))
29+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
=== tests/cases/compiler/uniqueSymbolAllowsIndexInObjectWithIndexSignature.ts ===
2+
// https://github.com/Microsoft/TypeScript/issues/21962
3+
export const SYM = Symbol('a unique symbol');
4+
>SYM : unique symbol
5+
>Symbol('a unique symbol') : unique symbol
6+
>Symbol : SymbolConstructor
7+
>'a unique symbol' : "a unique symbol"
8+
9+
export interface I {
10+
>I : I
11+
12+
[SYM]: 'sym';
13+
>[SYM] : "sym"
14+
>SYM : unique symbol
15+
16+
[x: string]: 'str';
17+
>x : string
18+
}
19+
20+
let a: I = {[SYM]: 'sym'}; // Expect ok
21+
>a : I
22+
>I : I
23+
>{[SYM]: 'sym'} : { [SYM]: "sym"; }
24+
>[SYM] : "sym"
25+
>SYM : unique symbol
26+
>'sym' : "sym"
27+
28+
let b: I = {[SYM]: 'str'}; // Expect error
29+
>b : I
30+
>I : I
31+
>{[SYM]: 'str'} : { [SYM]: "str"; }
32+
>[SYM] : "str"
33+
>SYM : unique symbol
34+
>'str' : "str"
35+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// @lib: es6
2+
// https://github.com/Microsoft/TypeScript/issues/21962
3+
export const SYM = Symbol('a unique symbol');
4+
5+
export interface I {
6+
[SYM]: 'sym';
7+
[x: string]: 'str';
8+
}
9+
10+
let a: I = {[SYM]: 'sym'}; // Expect ok
11+
let b: I = {[SYM]: 'str'}; // Expect error

0 commit comments

Comments
 (0)