Skip to content

Fix assignment to property of 'this' though index signature #31704

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

Merged
merged 3 commits into from
May 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion tests/baselines/reference/keyofAndIndexedAccess2.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}
Expand Down
6 changes: 4 additions & 2 deletions tests/baselines/reference/keyofAndIndexedAccess2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}
Expand Down Expand Up @@ -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";
}
}
73 changes: 38 additions & 35 deletions tests/baselines/reference/keyofAndIndexedAccess2.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
}
Expand All @@ -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))

9 changes: 8 additions & 1 deletion tests/baselines/reference/keyofAndIndexedAccess2.types
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ function fn4<K extends number>() {
>'abc' : "abc"
}

// Repro from #31439
// Repro from #31439 and #31691

export class c {
>c : c
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}
Expand Down