Merge pull request #31704 from microsoft/fixThisIndexSignatureAssignment

Fix assignment to property of 'this' though index signature
This commit is contained in:
Anders Hejlsberg 2019-05-31 12:49:42 -07:00 committed by GitHub
commit 487bd78ecc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 55 additions and 41 deletions

View File

@ -20192,7 +20192,7 @@ namespace ts {
markAliasReferenced(parentSymbol, node);
}
if (!prop) {
const indexInfo = assignmentKind === AssignmentKind.None || !isGenericObjectType(leftType) ? getIndexInfoOfType(apparentType, IndexKind.String) : undefined;
const indexInfo = assignmentKind === AssignmentKind.None || !isGenericObjectType(leftType) || isThisTypeParameter(leftType) ? getIndexInfoOfType(apparentType, IndexKind.String) : undefined;
if (!(indexInfo && indexInfo.type)) {
if (isJSLiteralType(leftType)) {
return anyType;

View File

@ -222,11 +222,12 @@ tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(108,5): error TS23
let y: ReadonlyArray<string>[K] = 'abc';
}
// Repro from #31439
// Repro from #31439 and #31691
export class c {
[x: string]: string;
constructor() {
this.a = "b";
this["a"] = "b";
}
}

View File

@ -137,11 +137,12 @@ function fn4<K extends number>() {
let y: ReadonlyArray<string>[K] = 'abc';
}
// Repro from #31439
// Repro from #31439 and #31691
export class c {
[x: string]: string;
constructor() {
this.a = "b";
this["a"] = "b";
}
}
@ -245,9 +246,10 @@ function fn4() {
let x = 'abc';
let y = 'abc';
}
// Repro from #31439
// Repro from #31439 and #31691
export class c {
constructor() {
this.a = "b";
this["a"] = "b";
}
}

View File

@ -503,7 +503,7 @@ function fn4<K extends number>() {
>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 133, 13))
}
// Repro from #31439
// Repro from #31439 and #31691
export class c {
>c : Symbol(c, Decl(keyofAndIndexedAccess2.ts, 136, 1))
@ -512,6 +512,9 @@ export class c {
>x : Symbol(x, Decl(keyofAndIndexedAccess2.ts, 141, 3))
constructor() {
this.a = "b";
>this : Symbol(c, Decl(keyofAndIndexedAccess2.ts, 136, 1))
this["a"] = "b";
>this : Symbol(c, Decl(keyofAndIndexedAccess2.ts, 136, 1))
}
@ -520,44 +523,44 @@ export class c {
// Repro from #31385
type Foo<T> = { [key: string]: { [K in keyof T]: K }[keyof T] };
>Foo : Symbol(Foo, Decl(keyofAndIndexedAccess2.ts, 145, 1))
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 149, 9))
>key : Symbol(key, Decl(keyofAndIndexedAccess2.ts, 149, 17))
>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 149, 34))
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 149, 9))
>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 149, 34))
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 149, 9))
>Foo : Symbol(Foo, Decl(keyofAndIndexedAccess2.ts, 146, 1))
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 150, 9))
>key : Symbol(key, Decl(keyofAndIndexedAccess2.ts, 150, 17))
>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 150, 34))
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 150, 9))
>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 150, 34))
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 150, 9))
type Bar<T> = { [key: string]: { [K in keyof T]: [K] }[keyof T] };
>Bar : Symbol(Bar, Decl(keyofAndIndexedAccess2.ts, 149, 64))
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 151, 9))
>key : Symbol(key, Decl(keyofAndIndexedAccess2.ts, 151, 17))
>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 151, 34))
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 151, 9))
>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 151, 34))
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 151, 9))
>Bar : Symbol(Bar, Decl(keyofAndIndexedAccess2.ts, 150, 64))
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 152, 9))
>key : Symbol(key, Decl(keyofAndIndexedAccess2.ts, 152, 17))
>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 152, 34))
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 152, 9))
>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 152, 34))
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 152, 9))
type Baz<T, Q extends Foo<T>> = { [K in keyof Q]: T[Q[K]] };
>Baz : Symbol(Baz, Decl(keyofAndIndexedAccess2.ts, 151, 66))
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 153, 9))
>Q : Symbol(Q, Decl(keyofAndIndexedAccess2.ts, 153, 11))
>Foo : Symbol(Foo, Decl(keyofAndIndexedAccess2.ts, 145, 1))
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 153, 9))
>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 153, 35))
>Q : Symbol(Q, Decl(keyofAndIndexedAccess2.ts, 153, 11))
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 153, 9))
>Q : Symbol(Q, Decl(keyofAndIndexedAccess2.ts, 153, 11))
>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 153, 35))
>Baz : Symbol(Baz, Decl(keyofAndIndexedAccess2.ts, 152, 66))
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 154, 9))
>Q : Symbol(Q, Decl(keyofAndIndexedAccess2.ts, 154, 11))
>Foo : Symbol(Foo, Decl(keyofAndIndexedAccess2.ts, 146, 1))
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 154, 9))
>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 154, 35))
>Q : Symbol(Q, Decl(keyofAndIndexedAccess2.ts, 154, 11))
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 154, 9))
>Q : Symbol(Q, Decl(keyofAndIndexedAccess2.ts, 154, 11))
>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 154, 35))
type Qux<T, Q extends Bar<T>> = { [K in keyof Q]: T[Q[K]["0"]] };
>Qux : Symbol(Qux, Decl(keyofAndIndexedAccess2.ts, 153, 60))
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 155, 9))
>Q : Symbol(Q, Decl(keyofAndIndexedAccess2.ts, 155, 11))
>Bar : Symbol(Bar, Decl(keyofAndIndexedAccess2.ts, 149, 64))
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 155, 9))
>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 155, 35))
>Q : Symbol(Q, Decl(keyofAndIndexedAccess2.ts, 155, 11))
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 155, 9))
>Q : Symbol(Q, Decl(keyofAndIndexedAccess2.ts, 155, 11))
>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 155, 35))
>Qux : Symbol(Qux, Decl(keyofAndIndexedAccess2.ts, 154, 60))
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 156, 9))
>Q : Symbol(Q, Decl(keyofAndIndexedAccess2.ts, 156, 11))
>Bar : Symbol(Bar, Decl(keyofAndIndexedAccess2.ts, 150, 64))
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 156, 9))
>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 156, 35))
>Q : Symbol(Q, Decl(keyofAndIndexedAccess2.ts, 156, 11))
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 156, 9))
>Q : Symbol(Q, Decl(keyofAndIndexedAccess2.ts, 156, 11))
>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 156, 35))

View File

@ -499,7 +499,7 @@ function fn4<K extends number>() {
>'abc' : "abc"
}
// Repro from #31439
// Repro from #31439 and #31691
export class c {
>c : c
@ -508,6 +508,13 @@ export class c {
>x : string
constructor() {
this.a = "b";
>this.a = "b" : "b"
>this.a : string
>this : this
>a : string
>"b" : "b"
this["a"] = "b";
>this["a"] = "b" : "b"
>this["a"] : string

View File

@ -139,11 +139,12 @@ function fn4<K extends number>() {
let y: ReadonlyArray<string>[K] = 'abc';
}
// Repro from #31439
// Repro from #31439 and #31691
export class c {
[x: string]: string;
constructor() {
this.a = "b";
this["a"] = "b";
}
}