-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Closed
Labels
BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issue
Milestone
Description
TypeScript Version: 4.2.0-dev.20201109 (online Playground)
Search Terms:
- key remapping
- keyof never
Code
Same code as below as Playground
type Equal<A, B> = (<T>() => T extends A ? 1 : 2) extends (<T>() => T extends B ? 1 : 2) ? true : false
type If<Cond extends boolean, Then, Else> = Cond extends true ? Then : Else
type GetKey<
S extends {},
V
> = keyof {
[TP in keyof S as Equal<S[TP], V> extends true ? TP : never]: any
}
type GetKeyWithIf<
S extends {},
V
> = keyof {
[TP in keyof S as If<Equal<S[TP], V>, TP, never>]: any
}
type GetObjWithIf<
S extends {},
V
> = {
[TP in keyof S as If<Equal<S[TP], V>, TP, never>]: any
}
type Task = {
isDone: boolean
}
type Schema = {
root: {
title: string
task: Task
}
Task: Task
}
// correctly returns `"Task"`
type Res1 = GetKey<Schema, Schema['root']['task']>
// incorrectly returns `never`
type Res2 = GetKeyWithIf<Schema, Schema['root']['task']>
// this weirdly works though
type Res3 = keyof GetObjWithIf<Schema, Schema['root']['task']>Expected behavior:
Res2 should also return "Task".
Actual behavior:
Res2 returns never but Res3 works.
Playground Link:
Related Issues:
- Key Remapping and keyof modifier #40833
- Keyof mapped with
astype resolving to never #41133 - Properly distribute over unions in keyof for mapped types with as clause #40837
PS: Dear issue triage folks, I'm not sure whether I hit the nail on its head in the issue title, so please feel free to adjust it to be more accurate/descriptive.
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issue