Skip to content

Commit 2d80253

Browse files
author
Andy
authored
Fix error message for implicit-any property in object literal with symbol key (#21883)
1 parent 67984c7 commit 2d80253

6 files changed

+50
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11203,7 +11203,7 @@ namespace ts {
1120311203
const t = getTypeOfSymbol(p);
1120411204
if (t.flags & TypeFlags.ContainsWideningType) {
1120511205
if (!reportWideningErrorsInType(t)) {
11206-
error(p.valueDeclaration, Diagnostics.Object_literal_s_property_0_implicitly_has_an_1_type, symbolName(p), typeToString(getWidenedType(t)));
11206+
error(p.valueDeclaration, Diagnostics.Object_literal_s_property_0_implicitly_has_an_1_type, symbolToString(p), typeToString(getWidenedType(t)));
1120711207
}
1120811208
errorReported = true;
1120911209
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
tests/cases/compiler/objectLiteralPropertyImplicitlyAny.ts(2,13): error TS7018: Object literal's property '[foo]' implicitly has an 'any' type.
2+
3+
4+
==== tests/cases/compiler/objectLiteralPropertyImplicitlyAny.ts (1 errors) ====
5+
const foo = Symbol.for("foo");
6+
const o = { [foo]: undefined };
7+
~~~~~~~~~~~~~~~~
8+
!!! error TS7018: Object literal's property '[foo]' implicitly has an 'any' type.
9+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//// [objectLiteralPropertyImplicitlyAny.ts]
2+
const foo = Symbol.for("foo");
3+
const o = { [foo]: undefined };
4+
5+
6+
//// [objectLiteralPropertyImplicitlyAny.js]
7+
const foo = Symbol.for("foo");
8+
const o = { [foo]: undefined };
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
=== tests/cases/compiler/objectLiteralPropertyImplicitlyAny.ts ===
2+
const foo = Symbol.for("foo");
3+
>foo : Symbol(foo, Decl(objectLiteralPropertyImplicitlyAny.ts, 0, 5))
4+
>Symbol.for : Symbol(SymbolConstructor.for, Decl(lib.es2015.symbol.d.ts, --, --))
5+
>Symbol : Symbol(Symbol, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --))
6+
>for : Symbol(SymbolConstructor.for, Decl(lib.es2015.symbol.d.ts, --, --))
7+
8+
const o = { [foo]: undefined };
9+
>o : Symbol(o, Decl(objectLiteralPropertyImplicitlyAny.ts, 1, 5))
10+
>foo : Symbol(foo, Decl(objectLiteralPropertyImplicitlyAny.ts, 0, 5))
11+
>undefined : Symbol(undefined)
12+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
=== tests/cases/compiler/objectLiteralPropertyImplicitlyAny.ts ===
2+
const foo = Symbol.for("foo");
3+
>foo : unique symbol
4+
>Symbol.for("foo") : unique symbol
5+
>Symbol.for : (key: string) => symbol
6+
>Symbol : SymbolConstructor
7+
>for : (key: string) => symbol
8+
>"foo" : "foo"
9+
10+
const o = { [foo]: undefined };
11+
>o : { [foo]: any; }
12+
>{ [foo]: undefined } : { [foo]: undefined; }
13+
>foo : unique symbol
14+
>undefined : undefined
15+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// @target: esnext
2+
// @noImplicitAny: true
3+
4+
const foo = Symbol.for("foo");
5+
const o = { [foo]: undefined };

0 commit comments

Comments
 (0)