You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
typeT1={[x: string]: unknown}// ^? type T1 = { [x: string]: unknown; }typeR1=keyofT1// ^? type R1 = string | numbertypeT2=Record<string,unknown>// ^? type T2 = { [x: string]: unknown; }typeR2=keyofT2// ^? type R2 = string
π Actual behavior
According to the type hint, the types of T1 and T2 are the same, but the results they get through keyof are not the same. According to the document, they should both get string | number .
Yeah, it's confusing that Record<string, T> is displayed as an index-signature type in type hints. It's actually a mapped type over string, which, while they behave similarly most of the time, is not the same thing as a string index signature.
π Search Terms
The Keyof Type Operator shows inconsistent behavior.
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play?#code/C4TwDgpgBAKgjFAvFA3lA2gDwFxQM7ABOAlgHYDmAurgK6kDWpA9gO6lQC+AsAFAD0fKEIB6Afl69QkKACUEyehBBMAZrDi8BQqGIk8p0GACYksiAGMmhACYAeAiQoAaKHUatSAPk2CR4npLg0DImCkqqsEY+2ro8QA
π» Code
π Actual behavior
According to the type hint, the types of
T1
andT2
are the same, but the results they get throughkeyof
are not the same. According to the document, they should both getstring | number
.Document link: https://www.typescriptlang.org/docs/handbook/2/keyof-types.html#handbook-content
π Expected behavior
Infer the correct type.
Additional information about the issue
In addition, it may also be related to #56888.
The text was updated successfully, but these errors were encountered: