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
{{ message }}
Repository navigation
TS 4.2 beta preserves "keyof" expressions in type displayΒ #42322
I assume this is the result of #42284, but this display seems much less informative than what we used to have:
Having used keyof in the past, I know that keyof Point is the same as "x" | "y". But displaying its type as keyof Point isn't very informative if you don't know that. I worry that this will make it much harder for new TypeScript users to understand how keyof works.
When there are two keys, it's probably preferable, but what about when there are a hundred? It's less clear then, and losing the information about where those keys are coming from is less recoverable. As a developer I can go to the definition of Point and read its keys, but there's no reverse operation that takes me to "a" type that has the keys "a" | "bold" | "div" | "form" | "h1" | "h2" | "h3" | ...
Fair enough, but with TS 4.2 is there any way to expand the type so that I can see what it actually is?
The unknown & trick doesn't seem to work here:
interfacePoint{x: number;y: number;}typeAxes=keyofPoint;// type is keyof PointtypeAxesExpand=unknown&Axes;// type is keyof PointtypeAxesExpand2=unknown&keyofPoint;// type is keyof Point
There is also the useful {[K in keyof T]: T[K]} to "simplify" objects. Maybe there should be a page in the docs about those? They are really useful to prettify type definitions and not very known as they should.
Edit: Or even, as they are related, a "Simplify"<T extends Record<string, unknown> | keyof any> = T extends keyof any ? Exclude<T, never> : {[K in keyof T]: T[K]}. I don't recall other functionalities like that, but they could also appear there.
The
When there are two keys, it's probably preferable, but what about when there are a hundred?
is a really good argument, but should be easier for new devs to know how they can "simplify" their types when wanted, as it really improves the DX some times.
Activity
RyanCavanaugh commentedon Jan 13, 2021
When there are two keys, it's probably preferable, but what about when there are a hundred? It's less clear then, and losing the information about where those keys are coming from is less recoverable. As a developer I can go to the definition of
Pointand read its keys, but there's no reverse operation that takes me to "a" type that has the keys"a" | "bold" | "div" | "form" | "h1" | "h2" | "h3" | ...danvk commentedon Jan 13, 2021
Fair enough, but with TS 4.2 is there any way to expand the type so that I can see what it actually is?
The
unknown &trick doesn't seem to work here:RyanCavanaugh commentedon Jan 13, 2021
RyanCavanaugh commentedon Jan 13, 2021
Or
danvk commentedon Jan 13, 2021
Thanks Ryan Cavanaugh (@RyanCavanaugh) !
ftzi commentedon Jan 16, 2021
There is also the useful
{[K in keyof T]: T[K]}to "simplify" objects. Maybe there should be a page in the docs about those? They are really useful to prettify type definitions and not very known as they should.Edit: Or even, as they are related, a
"Simplify"<T extends Record<string, unknown> | keyof any> = T extends keyof any ? Exclude<T, never> : {[K in keyof T]: T[K]}. I don't recall other functionalities like that, but they could also appear there.The
is a really good argument, but should be easier for new devs to know how they can "simplify" their types when wanted, as it really improves the DX some times.
typescript-bot commentedon Jan 18, 2021
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.