Skip to content

Literal tested as key of a certain type cannot be used as an index type #57109

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

Open
ghostinpeace opened this issue Jan 20, 2024 · 1 comment Β· May be fixed by #57113
Open

Literal tested as key of a certain type cannot be used as an index type #57109

ghostinpeace opened this issue Jan 20, 2024 · 1 comment Β· May be fixed by #57113
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@ghostinpeace
Copy link

πŸ”Ž Search Terms

"literal", "extends", "keyof", "cannot be used to index type", "conditional"

πŸ•— Version & Regression Information

Versions: ^5.0 (regression)

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.3.3&ssl=7&ssc=54&pln=1&pc=1#code/C4TwDgpgBAKlC8UDeBYAUFKBrAXFAziALYBGA9gDYA06mZwAFhAE57mUQCGAdlVAPT8oPek2bYIIAhAhF8UZhACOAVwCWigCZQAZmXGNoJFQHMowMsLCROzdAF906QbHDQA5ACIsn91ADGPNz0UCTQKvgQ2pzyPFBq3JoQAB7mbgB0ABQATACsAMwAHACU6KCQUADCCFDuWH4pwBCJ8liSZDqwUAD8sADade4AulB43BAAbiwA3AJCPCBAA

πŸ’» Code

type T = {
  k: symbol,
  other: boolean, // another key seems required for the bug to appear
}

// Type '"k"' cannot be used as an index type.(2538)
type C = 'k' extends keyof T ? T['k'] : never; // any

πŸ™ Actual behavior

An error is produced in the positive clause of the conditional type.
The resulting type is incorrect.

πŸ™‚ Expected behavior

No error.

Additional information about the issue

No response

@ghostinpeace ghostinpeace changed the title Literal tested as key of a certain type in condition cannot be used to index that type Literal tested as key of a certain type cannot be used to index that type Jan 20, 2024
@ghostinpeace ghostinpeace changed the title Literal tested as key of a certain type cannot be used to index that type Literal tested as key of a certain type cannot be used as an index type Jan 20, 2024
@craigphicks
Copy link

Weird. This works

type T = {
  k: symbol,
  other: boolean, // another key seems required for the bug to appear
}
type C<U=T> = 'k' extends keyof U ? U['k'] : never;
type X = C; // symbol

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Help Wanted You can do this labels Jan 22, 2024
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Jan 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants