Skip to content

Regression: Type cannot be used to index type 'this'. #31439

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

Closed
martinwebrant opened this issue May 17, 2019 · 1 comment · Fixed by #31454
Closed

Regression: Type cannot be used to index type 'this'. #31439

martinwebrant opened this issue May 17, 2019 · 1 comment · Fixed by #31454
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@martinwebrant
Copy link

TypeScript Version: 3.5.0-dev.20190517

Search Terms:
TS2536
Type cannot be used to index type 'this'.

Code

export class c {
  [x: string]: string;
  constructor() {
    this["a"] = "b";
  }
}

Expected behavior:
No errors

Actual behavior:
error TS2536: Type '"a"' cannot be used to index type 'this'.

Playground Link:
Works fine in playground but fails in 3.5.0.rc
https://www.typescriptlang.org/play/#src=class%20y%20%7B%0D%0A%20%20%20%20%5Bx%3A%20string%5D%3A%20string%3B%0D%0A%20%20%20%20constructor()%20%7B%0D%0A%20%20%20%20%20%20%20%20this%5B%22b%22%5D%20%3D%20%22a%22%3B%0D%0A%20%20%20%20%7D%0D%0A%7D%0D%0A

Related Issues:
Could not find any

@ahejlsberg
Copy link
Member

Hmm, not a great fan of this pattern, but I agree it is a regression we should fix. The cause of the issue is #30769, specifically that we now ignore index signatures in type parameter constraints on the target side of an assignment. We need to make an exception for this type parameters because actual type arguments are known to have an index signature (through inheritance).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants