diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 4768a357d2b..40998d9a633 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -6036,12 +6036,10 @@ namespace ts { const resolved = resolveStructuredTypeMembers(type); if ((relation === assignableRelation || relation === comparableRelation) && (type === globalObjectType || isEmptyObjectType(resolved)) || resolved.stringIndexInfo || + (resolved.numberIndexInfo && isNumericLiteralName(name)) || getPropertyOfType(type, name)) { return true; } - if (resolved.numberIndexInfo) { - return isNumericLiteralName(name); - } } else if (type.flags & TypeFlags.UnionOrIntersection) { for (const t of (type).types) { diff --git a/tests/baselines/reference/computedPropertyNamesContextualType7_ES5.js b/tests/baselines/reference/computedPropertyNamesContextualType7_ES5.js index 65cc057fa81..bc8966520f6 100644 --- a/tests/baselines/reference/computedPropertyNamesContextualType7_ES5.js +++ b/tests/baselines/reference/computedPropertyNamesContextualType7_ES5.js @@ -1,22 +1,26 @@ //// [computedPropertyNamesContextualType7_ES5.ts] interface I { - [s: number]: T; + [n: number]: T; +} +interface J { + [s: string]: T; } -declare function foo(obj: I): T +declare function foo(obj: I): T; +declare function g(obj: J): T; foo({ - 101: "", 0: () => { }, ["hi" + "bye"]: true, [0 + 1]: 0, [+"hi"]: [0] }); + +g({ p: "" }); //// [computedPropertyNamesContextualType7_ES5.js] foo((_a = { - 101: "", 0: function () { } }, _a["hi" + "bye"] = true, @@ -24,4 +28,5 @@ foo((_a = { _a[+"hi"] = [0], _a )); +g({ p: "" }); var _a; diff --git a/tests/baselines/reference/computedPropertyNamesContextualType7_ES5.symbols b/tests/baselines/reference/computedPropertyNamesContextualType7_ES5.symbols index 562b238cdac..d6889f9912d 100644 --- a/tests/baselines/reference/computedPropertyNamesContextualType7_ES5.symbols +++ b/tests/baselines/reference/computedPropertyNamesContextualType7_ES5.symbols @@ -3,26 +3,45 @@ interface I { >I : Symbol(I, Decl(computedPropertyNamesContextualType7_ES5.ts, 0, 0)) >T : Symbol(T, Decl(computedPropertyNamesContextualType7_ES5.ts, 0, 12)) - [s: number]: T; ->s : Symbol(s, Decl(computedPropertyNamesContextualType7_ES5.ts, 1, 5)) + [n: number]: T; +>n : Symbol(n, Decl(computedPropertyNamesContextualType7_ES5.ts, 1, 5)) >T : Symbol(T, Decl(computedPropertyNamesContextualType7_ES5.ts, 0, 12)) } +interface J { +>J : Symbol(J, Decl(computedPropertyNamesContextualType7_ES5.ts, 2, 1)) +>T : Symbol(T, Decl(computedPropertyNamesContextualType7_ES5.ts, 3, 12)) -declare function foo(obj: I): T ->foo : Symbol(foo, Decl(computedPropertyNamesContextualType7_ES5.ts, 2, 1)) ->T : Symbol(T, Decl(computedPropertyNamesContextualType7_ES5.ts, 4, 21)) ->obj : Symbol(obj, Decl(computedPropertyNamesContextualType7_ES5.ts, 4, 24)) + [s: string]: T; +>s : Symbol(s, Decl(computedPropertyNamesContextualType7_ES5.ts, 4, 5)) +>T : Symbol(T, Decl(computedPropertyNamesContextualType7_ES5.ts, 3, 12)) +} + +declare function foo(obj: I): T; +>foo : Symbol(foo, Decl(computedPropertyNamesContextualType7_ES5.ts, 5, 1)) +>T : Symbol(T, Decl(computedPropertyNamesContextualType7_ES5.ts, 7, 21)) +>obj : Symbol(obj, Decl(computedPropertyNamesContextualType7_ES5.ts, 7, 24)) >I : Symbol(I, Decl(computedPropertyNamesContextualType7_ES5.ts, 0, 0)) ->T : Symbol(T, Decl(computedPropertyNamesContextualType7_ES5.ts, 4, 21)) ->T : Symbol(T, Decl(computedPropertyNamesContextualType7_ES5.ts, 4, 21)) +>T : Symbol(T, Decl(computedPropertyNamesContextualType7_ES5.ts, 7, 21)) +>T : Symbol(T, Decl(computedPropertyNamesContextualType7_ES5.ts, 7, 21)) + +declare function g(obj: J): T; +>g : Symbol(g, Decl(computedPropertyNamesContextualType7_ES5.ts, 7, 38)) +>T : Symbol(T, Decl(computedPropertyNamesContextualType7_ES5.ts, 8, 19)) +>obj : Symbol(obj, Decl(computedPropertyNamesContextualType7_ES5.ts, 8, 22)) +>J : Symbol(J, Decl(computedPropertyNamesContextualType7_ES5.ts, 2, 1)) +>T : Symbol(T, Decl(computedPropertyNamesContextualType7_ES5.ts, 8, 19)) +>T : Symbol(T, Decl(computedPropertyNamesContextualType7_ES5.ts, 8, 19)) foo({ ->foo : Symbol(foo, Decl(computedPropertyNamesContextualType7_ES5.ts, 2, 1)) +>foo : Symbol(foo, Decl(computedPropertyNamesContextualType7_ES5.ts, 5, 1)) - 101: "", 0: () => { }, ["hi" + "bye"]: true, [0 + 1]: 0, [+"hi"]: [0] }); +g({ p: "" }); +>g : Symbol(g, Decl(computedPropertyNamesContextualType7_ES5.ts, 7, 38)) +>p : Symbol(p, Decl(computedPropertyNamesContextualType7_ES5.ts, 17, 3)) + diff --git a/tests/baselines/reference/computedPropertyNamesContextualType7_ES5.types b/tests/baselines/reference/computedPropertyNamesContextualType7_ES5.types index 53c8f805c57..64367864881 100644 --- a/tests/baselines/reference/computedPropertyNamesContextualType7_ES5.types +++ b/tests/baselines/reference/computedPropertyNamesContextualType7_ES5.types @@ -3,12 +3,20 @@ interface I { >I : I >T : T - [s: number]: T; ->s : number + [n: number]: T; +>n : number +>T : T +} +interface J { +>J : J +>T : T + + [s: string]: T; +>s : string >T : T } -declare function foo(obj: I): T +declare function foo(obj: I): T; >foo : (obj: I) => T >T : T >obj : I @@ -16,13 +24,18 @@ declare function foo(obj: I): T >T : T >T : T -foo({ ->foo({ 101: "", 0: () => { }, ["hi" + "bye"]: true, [0 + 1]: 0, [+"hi"]: [0]}) : string | (() => void) | number | number[] ->foo : (obj: I) => T ->{ 101: "", 0: () => { }, ["hi" + "bye"]: true, [0 + 1]: 0, [+"hi"]: [0]} : { [x: string]: string | (() => void) | boolean | number | number[]; [x: number]: string | (() => void) | number | number[]; 0: () => void; 101: string; } +declare function g(obj: J): T; +>g : (obj: J) => T +>T : T +>obj : J +>J : J +>T : T +>T : T - 101: "", ->"" : string +foo({ +>foo({ 0: () => { }, ["hi" + "bye"]: true, [0 + 1]: 0, [+"hi"]: [0]}) : (() => void) | number | number[] +>foo : (obj: I) => T +>{ 0: () => { }, ["hi" + "bye"]: true, [0 + 1]: 0, [+"hi"]: [0]} : { [x: string]: (() => void) | boolean | number | number[]; [x: number]: (() => void) | number | number[]; 0: () => void; } 0: () => { }, >() => { } : () => void @@ -47,3 +60,10 @@ foo({ }); +g({ p: "" }); +>g({ p: "" }) : string +>g : (obj: J) => T +>{ p: "" } : { p: string; } +>p : string +>"" : string + diff --git a/tests/baselines/reference/computedPropertyNamesContextualType7_ES6.js b/tests/baselines/reference/computedPropertyNamesContextualType7_ES6.js index 0b515dba7ca..273c15018b6 100644 --- a/tests/baselines/reference/computedPropertyNamesContextualType7_ES6.js +++ b/tests/baselines/reference/computedPropertyNamesContextualType7_ES6.js @@ -1,24 +1,29 @@ //// [computedPropertyNamesContextualType7_ES6.ts] interface I { - [s: number]: T; + [n: number]: T; +} +interface J { + [s: string]: T; } -declare function foo(obj: I): T +declare function foo(obj: I): T; +declare function g(obj: J): T; foo({ - 101: "", 0: () => { }, ["hi" + "bye"]: true, [0 + 1]: 0, [+"hi"]: [0] }); + +g({ p: "" }); //// [computedPropertyNamesContextualType7_ES6.js] foo({ - 101: "", 0: () => { }, ["hi" + "bye"]: true, [0 + 1]: 0, [+"hi"]: [0] }); +g({ p: "" }); diff --git a/tests/baselines/reference/computedPropertyNamesContextualType7_ES6.symbols b/tests/baselines/reference/computedPropertyNamesContextualType7_ES6.symbols index ca0c2f12a80..d04c8f913bd 100644 --- a/tests/baselines/reference/computedPropertyNamesContextualType7_ES6.symbols +++ b/tests/baselines/reference/computedPropertyNamesContextualType7_ES6.symbols @@ -3,26 +3,45 @@ interface I { >I : Symbol(I, Decl(computedPropertyNamesContextualType7_ES6.ts, 0, 0)) >T : Symbol(T, Decl(computedPropertyNamesContextualType7_ES6.ts, 0, 12)) - [s: number]: T; ->s : Symbol(s, Decl(computedPropertyNamesContextualType7_ES6.ts, 1, 5)) + [n: number]: T; +>n : Symbol(n, Decl(computedPropertyNamesContextualType7_ES6.ts, 1, 5)) >T : Symbol(T, Decl(computedPropertyNamesContextualType7_ES6.ts, 0, 12)) } +interface J { +>J : Symbol(J, Decl(computedPropertyNamesContextualType7_ES6.ts, 2, 1)) +>T : Symbol(T, Decl(computedPropertyNamesContextualType7_ES6.ts, 3, 12)) -declare function foo(obj: I): T ->foo : Symbol(foo, Decl(computedPropertyNamesContextualType7_ES6.ts, 2, 1)) ->T : Symbol(T, Decl(computedPropertyNamesContextualType7_ES6.ts, 4, 21)) ->obj : Symbol(obj, Decl(computedPropertyNamesContextualType7_ES6.ts, 4, 24)) + [s: string]: T; +>s : Symbol(s, Decl(computedPropertyNamesContextualType7_ES6.ts, 4, 5)) +>T : Symbol(T, Decl(computedPropertyNamesContextualType7_ES6.ts, 3, 12)) +} + +declare function foo(obj: I): T; +>foo : Symbol(foo, Decl(computedPropertyNamesContextualType7_ES6.ts, 5, 1)) +>T : Symbol(T, Decl(computedPropertyNamesContextualType7_ES6.ts, 7, 21)) +>obj : Symbol(obj, Decl(computedPropertyNamesContextualType7_ES6.ts, 7, 24)) >I : Symbol(I, Decl(computedPropertyNamesContextualType7_ES6.ts, 0, 0)) ->T : Symbol(T, Decl(computedPropertyNamesContextualType7_ES6.ts, 4, 21)) ->T : Symbol(T, Decl(computedPropertyNamesContextualType7_ES6.ts, 4, 21)) +>T : Symbol(T, Decl(computedPropertyNamesContextualType7_ES6.ts, 7, 21)) +>T : Symbol(T, Decl(computedPropertyNamesContextualType7_ES6.ts, 7, 21)) + +declare function g(obj: J): T; +>g : Symbol(g, Decl(computedPropertyNamesContextualType7_ES6.ts, 7, 38)) +>T : Symbol(T, Decl(computedPropertyNamesContextualType7_ES6.ts, 8, 19)) +>obj : Symbol(obj, Decl(computedPropertyNamesContextualType7_ES6.ts, 8, 22)) +>J : Symbol(J, Decl(computedPropertyNamesContextualType7_ES6.ts, 2, 1)) +>T : Symbol(T, Decl(computedPropertyNamesContextualType7_ES6.ts, 8, 19)) +>T : Symbol(T, Decl(computedPropertyNamesContextualType7_ES6.ts, 8, 19)) foo({ ->foo : Symbol(foo, Decl(computedPropertyNamesContextualType7_ES6.ts, 2, 1)) +>foo : Symbol(foo, Decl(computedPropertyNamesContextualType7_ES6.ts, 5, 1)) - 101: "", 0: () => { }, ["hi" + "bye"]: true, [0 + 1]: 0, [+"hi"]: [0] }); +g({ p: "" }); +>g : Symbol(g, Decl(computedPropertyNamesContextualType7_ES6.ts, 7, 38)) +>p : Symbol(p, Decl(computedPropertyNamesContextualType7_ES6.ts, 17, 3)) + diff --git a/tests/baselines/reference/computedPropertyNamesContextualType7_ES6.types b/tests/baselines/reference/computedPropertyNamesContextualType7_ES6.types index f2eefe7405e..07aeda807b0 100644 --- a/tests/baselines/reference/computedPropertyNamesContextualType7_ES6.types +++ b/tests/baselines/reference/computedPropertyNamesContextualType7_ES6.types @@ -3,12 +3,20 @@ interface I { >I : I >T : T - [s: number]: T; ->s : number + [n: number]: T; +>n : number +>T : T +} +interface J { +>J : J +>T : T + + [s: string]: T; +>s : string >T : T } -declare function foo(obj: I): T +declare function foo(obj: I): T; >foo : (obj: I) => T >T : T >obj : I @@ -16,13 +24,18 @@ declare function foo(obj: I): T >T : T >T : T -foo({ ->foo({ 101: "", 0: () => { }, ["hi" + "bye"]: true, [0 + 1]: 0, [+"hi"]: [0]}) : string | (() => void) | number | number[] ->foo : (obj: I) => T ->{ 101: "", 0: () => { }, ["hi" + "bye"]: true, [0 + 1]: 0, [+"hi"]: [0]} : { [x: string]: string | (() => void) | boolean | number | number[]; [x: number]: string | (() => void) | number | number[]; 0: () => void; 101: string; } +declare function g(obj: J): T; +>g : (obj: J) => T +>T : T +>obj : J +>J : J +>T : T +>T : T - 101: "", ->"" : string +foo({ +>foo({ 0: () => { }, ["hi" + "bye"]: true, [0 + 1]: 0, [+"hi"]: [0]}) : (() => void) | number | number[] +>foo : (obj: I) => T +>{ 0: () => { }, ["hi" + "bye"]: true, [0 + 1]: 0, [+"hi"]: [0]} : { [x: string]: (() => void) | boolean | number | number[]; [x: number]: (() => void) | number | number[]; 0: () => void; } 0: () => { }, >() => { } : () => void @@ -47,3 +60,10 @@ foo({ }); +g({ p: "" }); +>g({ p: "" }) : string +>g : (obj: J) => T +>{ p: "" } : { p: string; } +>p : string +>"" : string + diff --git a/tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType7_ES5.ts b/tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType7_ES5.ts index 4f124ece6d2..6d98092b968 100644 --- a/tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType7_ES5.ts +++ b/tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType7_ES5.ts @@ -1,14 +1,19 @@ // @target: es5 interface I { - [s: number]: T; + [n: number]: T; +} +interface J { + [s: string]: T; } -declare function foo(obj: I): T +declare function foo(obj: I): T; +declare function g(obj: J): T; foo({ - 101: "", 0: () => { }, ["hi" + "bye"]: true, [0 + 1]: 0, [+"hi"]: [0] }); + +g({ p: "" }); diff --git a/tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType7_ES6.ts b/tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType7_ES6.ts index 2a7a476ca7a..459ce1471b4 100644 --- a/tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType7_ES6.ts +++ b/tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType7_ES6.ts @@ -1,14 +1,19 @@ // @target: es6 interface I { - [s: number]: T; + [n: number]: T; +} +interface J { + [s: string]: T; } -declare function foo(obj: I): T +declare function foo(obj: I): T; +declare function g(obj: J): T; foo({ - 101: "", 0: () => { }, ["hi" + "bye"]: true, [0 + 1]: 0, [+"hi"]: [0] }); + +g({ p: "" });