Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Computed names in declarations files are resolved even when non-literal, preserve computed names when expressions are entity names #60052

Merged
112 changes: 99 additions & 13 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5883,7 +5883,7 @@ export interface EmitResolver {
getDeclarationStatementsForSourceFile(node: SourceFile, flags: NodeBuilderFlags, internalFlags: InternalNodeBuilderFlags, tracker: SymbolTracker): Statement[] | undefined;
isImportRequiredByAugmentation(decl: ImportDeclaration): boolean;
isDefinitelyReferenceToGlobalSymbolObject(node: Node): boolean;
createLateBoundIndexSignatures(cls: ClassLikeDeclaration, enclosingDeclaration: Node, flags: NodeBuilderFlags, internalFlags: InternalNodeBuilderFlags, tracker: SymbolTracker): IndexSignatureDeclaration[] | undefined;
createLateBoundIndexSignatures(cls: ClassLikeDeclaration, enclosingDeclaration: Node, flags: NodeBuilderFlags, internalFlags: InternalNodeBuilderFlags, tracker: SymbolTracker): (IndexSignatureDeclaration | PropertyDeclaration)[] | undefined;
}

// dprint-ignore
Expand Down Expand Up @@ -7017,11 +7017,14 @@ export const enum IndexKind {
Number,
}

export type ElementWithComputedPropertyName = (ClassElement | ObjectLiteralElement) & { name: ComputedPropertyName; };

export interface IndexInfo {
keyType: Type;
type: Type;
isReadonly: boolean;
declaration?: IndexSignatureDeclaration;
components?: ElementWithComputedPropertyName[];
}

/** @internal */
Expand Down
12 changes: 6 additions & 6 deletions tests/baselines/reference/ES5SymbolProperty1.types
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ var Symbol: SymbolConstructor;
> : ^^^^^^^^^^^^^^^^^

var obj = {
>obj : { [x: string]: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^
>{ [Symbol.foo]: 0} : { [x: string]: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^
>obj : { [Symbol.foo]: number; }
> : ^^ ^^^^^^ ^^
>{ [Symbol.foo]: 0} : { [Symbol.foo]: number; }
> : ^^ ^^^^^^ ^^

[Symbol.foo]: 0
>[Symbol.foo] : number
Expand All @@ -32,8 +32,8 @@ var obj = {
obj[Symbol.foo];
>obj[Symbol.foo] : number
> : ^^^^^^
>obj : { [x: string]: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^
>obj : { [Symbol.foo]: number; }
> : ^^ ^^^^^^ ^^
>Symbol.foo : string
> : ^^^^^^
>Symbol : SymbolConstructor
Expand Down
4 changes: 4 additions & 0 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6850,11 +6850,15 @@ declare namespace ts {
String = 0,
Number = 1,
}
type ElementWithComputedPropertyName = (ClassElement | ObjectLiteralElement) & {
name: ComputedPropertyName;
};
interface IndexInfo {
keyType: Type;
type: Type;
isReadonly: boolean;
declaration?: IndexSignatureDeclaration;
components?: ElementWithComputedPropertyName[];
}
enum InferencePriority {
None = 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,12 @@ function foo8(y = (async () => z)(), z = 1) {

// error - used as computed name of method
function foo9(y = {[z]() { return z; }}, z = 1) {
>foo9 : (y?: { [x: number]: () => number; }, z?: number) => void
> : ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^
>y : { [x: number]: () => number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>{[z]() { return z; }} : { [x: number]: () => number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>foo9 : (y?: { [z]: () => number; }, z?: number) => void
> : ^ ^^^^^ ^^^^^^^^^^^^ ^^^^ ^^^^^^^^^^^^^^^^^^
>y : { [z]: () => number; }
> : ^^ ^^^^^^^^^^^^ ^^
>{[z]() { return z; }} : { [z]: () => number; }
> : ^^ ^^^^^^^^^^^^ ^^
>[z] : () => number
> : ^^^^^^^^^^^^
>z : number
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
capturedParametersInInitializers2.ts(3,20): error TS2373: Parameter 'y' cannot reference identifier 'x' declared after it.
capturedParametersInInitializers2.ts(4,14): error TS2373: Parameter 'y' cannot reference identifier 'x' declared after it.
capturedParametersInInitializers2.ts(6,10): error TS2373: Parameter 'y' cannot reference identifier 'z' declared after it.
capturedParametersInInitializers2.ts(13,26): error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
capturedParametersInInitializers2.ts(13,27): error TS2373: Parameter 'y' cannot reference identifier 'x' declared after it.


==== capturedParametersInInitializers2.ts (5 errors) ====
==== capturedParametersInInitializers2.ts (4 errors) ====
function foo(
y = class {
static c = x;
Expand All @@ -25,8 +24,6 @@ capturedParametersInInitializers2.ts(13,27): error TS2373: Parameter 'y' cannot
y.c
}
function foo2(y = class {[x] = x}, x = 1) {
~~~
!!! error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
~
!!! error TS2373: Parameter 'y' cannot reference identifier 'x' declared after it.
}
24 changes: 12 additions & 12 deletions tests/baselines/reference/checkJsObjectLiteralIndexSignatures.types
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ let s = `${n}`;
> : ^^^^^^

const numericIndex = { [n]: 1 };
>numericIndex : { [x: number]: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^
>{ [n]: 1 } : { [x: number]: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^
>numericIndex : { [n]: number; }
> : ^^ ^^^^^^ ^^
>{ [n]: 1 } : { [n]: number; }
> : ^^ ^^^^^^ ^^
>[n] : number
> : ^^^^^^
>n : number
Expand All @@ -42,18 +42,18 @@ numericIndex[n].toFixed();
> : ^ ^^^ ^^^^^
>numericIndex[n] : number
> : ^^^^^^
>numericIndex : { [x: number]: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^
>numericIndex : { [n]: number; }
> : ^^ ^^^^^^ ^^
>n : number
> : ^^^^^^
>toFixed : (fractionDigits?: number) => string
> : ^ ^^^ ^^^^^

const stringIndex = { [s]: 1 };
>stringIndex : { [x: string]: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^
>{ [s]: 1 } : { [x: string]: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^
>stringIndex : { [s]: number; }
> : ^^ ^^^^^^ ^^
>{ [s]: 1 } : { [s]: number; }
> : ^^ ^^^^^^ ^^
>[s] : number
> : ^^^^^^
>s : string
Expand All @@ -68,8 +68,8 @@ stringIndex[s].toFixed();
> : ^ ^^^ ^^^^^
>stringIndex[s] : number
> : ^^^^^^
>stringIndex : { [x: string]: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^
>stringIndex : { [s]: number; }
> : ^^ ^^^^^^ ^^
>s : string
> : ^^^^^^
>toFixed : (fractionDigits?: number) => string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ export const Mixer = Mix(class {


//// [classNonUniqueSymbolMethodHasSymbolIndexer.d.ts]
declare const a: symbol;
export declare class A {
[x: symbol]: () => number;
[a]: () => number;
}
export declare const Mixer: {
new (): {
[x: symbol]: () => number;
[a]: () => number;
};
} & (new (...args: any[]) => {
mixed: true;
});
export {};
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ function f (m: string) {
> : ^^^^^^

[1, 2, 3].map(i => {
>[1, 2, 3].map(i => { return true? { [m]: i } : { [m]: i + 1 } }) : { [x: string]: number; }[]
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^
>[1, 2, 3].map(i => { return true? { [m]: i } : { [m]: i + 1 } }) : { [m]: number; }[]
> : ^^ ^^^^^^ ^^^^
>[1, 2, 3].map : <U>(callbackfn: (value: number, index: number, array: number[]) => U, thisArg?: any) => U[]
> : ^ ^^ ^^^ ^^^^^^^^^^ ^^ ^^ ^^^^^^^^^^^^^^^^^^ ^^^ ^^^^^^^^
>[1, 2, 3] : number[]
Expand All @@ -22,26 +22,26 @@ function f (m: string) {
> : ^
>map : <U>(callbackfn: (value: number, index: number, array: number[]) => U, thisArg?: any) => U[]
> : ^ ^^ ^^^ ^^^^^^^^^^ ^^ ^^ ^^^^^^^^^^^^^^^^^^ ^^^ ^^^^^^^^
>i => { return true? { [m]: i } : { [m]: i + 1 } } : (i: number) => { [x: string]: number; }
> : ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>i => { return true? { [m]: i } : { [m]: i + 1 } } : (i: number) => { [m]: number; }
> : ^ ^^^^^^^^^^^^^^^ ^^^^^^ ^^
>i : number
> : ^^^^^^

return true? { [m]: i } : { [m]: i + 1 }
>true? { [m]: i } : { [m]: i + 1 } : { [x: string]: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^
>true? { [m]: i } : { [m]: i + 1 } : { [m]: number; }
> : ^^ ^^^^^^ ^^
>true : true
> : ^^^^
>{ [m]: i } : { [x: string]: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^
>{ [m]: i } : { [m]: number; }
> : ^^ ^^^^^^ ^^
>[m] : number
> : ^^^^^^
>m : string
> : ^^^^^^
>i : number
> : ^^^^^^
>{ [m]: i + 1 } : { [x: string]: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^
>{ [m]: i + 1 } : { [m]: number; }
> : ^^ ^^^^^^ ^^
>[m] : number
> : ^^^^^^
>m : string
Expand Down
5 changes: 1 addition & 4 deletions tests/baselines/reference/complicatedPrivacy.errors.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
complicatedPrivacy.ts(11,24): error TS1054: A 'get' accessor cannot have parameters.
complicatedPrivacy.ts(35,5): error TS1170: A computed property name in a type literal must refer to an expression whose type is a literal type or a 'unique symbol' type.
complicatedPrivacy.ts(35,6): error TS2693: 'number' only refers to a type, but is being used as a value here.
complicatedPrivacy.ts(73,55): error TS2694: Namespace 'mglo5' has no exported member 'i6'.


==== complicatedPrivacy.ts (4 errors) ====
==== complicatedPrivacy.ts (3 errors) ====
module m1 {
export module m2 {

Expand Down Expand Up @@ -42,8 +41,6 @@ complicatedPrivacy.ts(73,55): error TS2694: Namespace 'mglo5' has no exported me
export function f4(arg1:
{
[number]: C1; // Used to be indexer, now it is a computed property
~~~~~~~~
!!! error TS1170: A computed property name in a type literal must refer to an expression whose type is a literal type or a 'unique symbol' type.
~~~~~~
!!! error TS2693: 'number' only refers to a type, but is being used as a value here.
}) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ let [{[foo.toExponential()]: bar7}] = [{bar: "bar"}];
> : ^^^^^^^^^^^^^^^^
>{[foo]: bar} = {bar: "bar"} : { bar: string; }
> : ^^^^^^^^^^^^^^^^
>{[foo]: bar} : { [x: string]: any; }
> : ^^^^^^^^^^^^^^^^^^^^^
>{[foo]: bar} : { [foo]: any; }
> : ^^ ^^^ ^^
>[foo] : any
> : ^^^
>foo : string
Expand Down Expand Up @@ -241,10 +241,10 @@ let [{[foo.toExponential()]: bar7}] = [{bar: "bar"}];
[{[foo]: bar4}] = [{bar: "bar"}];
>[{[foo]: bar4}] = [{bar: "bar"}] : [{ bar: string; }]
> : ^^^^^^^^^^^^^^^^^^
>[{[foo]: bar4}] : [{ [x: string]: any; }]
> : ^^^^^^^^^^^^^^^^^^^^^^^
>{[foo]: bar4} : { [x: string]: any; }
> : ^^^^^^^^^^^^^^^^^^^^^
>[{[foo]: bar4}] : [{ [foo]: any; }]
> : ^^^ ^^^ ^^^
>{[foo]: bar4} : { [foo]: any; }
> : ^^ ^^^ ^^
>[foo] : any
> : ^^^
>foo : string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ let [{[foo.toExponential()]: bar7}] = [{bar: "bar"}];
> : ^^^^^^^^^^^^^^^^
>{[foo]: bar} = {bar: "bar"} : { bar: string; }
> : ^^^^^^^^^^^^^^^^
>{[foo]: bar} : { [x: string]: any; }
> : ^^^^^^^^^^^^^^^^^^^^^
>{[foo]: bar} : { [foo]: any; }
> : ^^ ^^^ ^^
>[foo] : any
> : ^^^
>foo : string
Expand Down Expand Up @@ -253,10 +253,10 @@ let [{[foo.toExponential()]: bar7}] = [{bar: "bar"}];
[{[foo]: bar4}] = [{bar: "bar"}];
>[{[foo]: bar4}] = [{bar: "bar"}] : [{ bar: string; }]
> : ^^^^^^^^^^^^^^^^^^
>[{[foo]: bar4}] : [{ [x: string]: any; }]
> : ^^^^^^^^^^^^^^^^^^^^^^^
>{[foo]: bar4} : { [x: string]: any; }
> : ^^^^^^^^^^^^^^^^^^^^^
>[{[foo]: bar4}] : [{ [foo]: any; }]
> : ^^^ ^^^ ^^^
>{[foo]: bar4} : { [foo]: any; }
> : ^^ ^^^ ^^
>[foo] : any
> : ^^^
>foo : string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
computedPropertiesNarrowed.ts(5,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
computedPropertiesNarrowed.ts(11,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
computedPropertiesNarrowed.ts(18,20): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
computedPropertiesNarrowed.ts(20,5): error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations.
computedPropertiesNarrowed.ts(22,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
computedPropertiesNarrowed.ts(26,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
computedPropertiesNarrowed.ts(31,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
Expand All @@ -9,7 +10,7 @@ computedPropertiesNarrowed.ts(42,5): error TS9038: Computed property names on cl
computedPropertiesNarrowed.ts(47,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.


==== computedPropertiesNarrowed.ts (9 errors) ====
==== computedPropertiesNarrowed.ts (10 errors) ====
const x: 0 | 1 = Math.random()? 0: 1;
declare function assert(n: number): asserts n is 1;
assert(x);
Expand Down Expand Up @@ -39,6 +40,9 @@ computedPropertiesNarrowed.ts(47,5): error TS9038: Computed property names on cl
!!! related TS9027 computedPropertiesNarrowed.ts:18:12: Add a type annotation to the variable o32.

let u = Symbol();
~
!!! error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations.
!!! related TS9027 computedPropertiesNarrowed.ts:20:5: Add a type annotation to the variable u.
export let o4 = {
[u]: 1 // Should error, nut a unique symbol
~~~
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/computedPropertiesNarrowed.types
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ let u = Symbol();
> : ^^^^^^^^^^^^^^^^^

export let o4 = {
>o4 : { [x: symbol]: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^
>{ [u]: 1 // Should error, nut a unique symbol} : { [x: symbol]: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^
>o4 : { [u]: number; }
> : ^^ ^^^^^^ ^^
>{ [u]: 1 // Should error, nut a unique symbol} : { [u]: number; }
> : ^^ ^^^^^^ ^^

[u]: 1 // Should error, nut a unique symbol
>[u] : number
Expand Down
11 changes: 1 addition & 10 deletions tests/baselines/reference/computedPropertyNames12_ES5.errors.txt
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
computedPropertyNames12_ES5.ts(5,5): error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
computedPropertyNames12_ES5.ts(6,5): error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
computedPropertyNames12_ES5.ts(7,12): error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
computedPropertyNames12_ES5.ts(8,5): error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
computedPropertyNames12_ES5.ts(9,5): error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
computedPropertyNames12_ES5.ts(9,5): error TS2411: Property '[+s]' of type 'string' is not assignable to 'number' index type 'number'.
computedPropertyNames12_ES5.ts(9,5): error TS2411: Property '[+s]' of type 'string' is not assignable to 'string' index type 'number'.
computedPropertyNames12_ES5.ts(12,5): error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
computedPropertyNames12_ES5.ts(13,12): error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
computedPropertyNames12_ES5.ts(15,12): error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.


==== computedPropertyNames12_ES5.ts (10 errors) ====
==== computedPropertyNames12_ES5.ts (7 errors) ====
var s: string;
var n: number;
var a: any;
class C {
[s]: number;
~~~
!!! error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
[n] = n;
~~~
!!! error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
static [s + s]: string;
~~~~~~~
!!! error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
Expand All @@ -37,8 +30,6 @@ computedPropertyNames12_ES5.ts(15,12): error TS1166: A computed property name in
static [""]: number;
[0]: number;
[a]: number;
~~~
!!! error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
static [<any>true]: number;
~~~~~~~~~~~
!!! error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
Expand Down
Loading
Loading