Skip to content

Key Remapping and keyof modifier #40833

@shauns

Description

@shauns

TypeScript Version: 4.1.0-beta

Using the new key remapping feature, with a remapped object with never keys (for omitting). Passing the remapped object to keyof returns never, instead of the keys from the remapped object. Checks with the remapped object itself work as expected - its only when then using keyof do they fail.

Search Terms:

  • key remapping
  • keyof with never

Code

type Example = {foo: string, bar: number}
  
type PickByValueType<T, U> = {
  [K in keyof T as T[K] extends U ? K : never]: T[K]
}

type T1 = PickByValueType<Example, string>
const e1: T1 = {
    foo: "hello"
}
type T2 = keyof T1
const e2: T2 = "foo"

Expected behavior:

keyof T1 should be "foo"

Actual behavior:

keyof T1 is never

T1 correctly picks out the expected keys based on the condition check, but when ran through keyof it seems to lose the "set" keys.

Playground Link:

https://www.typescriptlang.org/play?ts=4.1.0-pr-40336-88&ssl=4&ssc=41&pln=4&pc=46#code/C4TwDgpgBAogHgQwLZgDbQLxQN4DMD2+AXFAM7ABOAlgHYDmANFAEYIUk0CuSzEFAvgCgoUQaEhQAClQDGAawBCIAGoJUnCABVwEADyamAVQB8ULNmFQA2gGkotKHIgh8uKJqgJS72wF0oEHDAEDQAJt6GUAD8UHYcEABufL4kmn6CQmI67gCMZlKyiipqGtqQuvDIaBBM5NT0xoIy+DTkATmpeeaWIgTEUABEABYQqKj4AxlZEpoATPlOLm6aOU0tbRCzqfNYA32TQA

Related Issues:

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions