Skip to content
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

TS 4.2 beta preserves "keyof" expressions in type display #42322

Closed
danvk opened this issue Jan 13, 2021 · 7 comments
Closed

TS 4.2 beta preserves "keyof" expressions in type display #42322

danvk opened this issue Jan 13, 2021 · 7 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@danvk
Copy link
Contributor

danvk commented Jan 13, 2021

Bug Report

πŸ”Ž Search Terms

  • keyof 4.2

πŸ•— Version & Regression Information

$ tsc --version
Version 4.2.0-dev
  • This changed between versions 4.1.3 and 4.2.0-beta

⏯ Playground Link

playground

πŸ’» Code

interface Point {
  x: number;
  y: number;
}
type Axes = keyof Point;

Mouse over Axes.

πŸ™ Actual behavior

Displays keyof Point

πŸ™‚ Expected behavior

Displays "x" | "y".

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.

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Jan 13, 2021
@RyanCavanaugh
Copy link
Member

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" | ...

@danvk
Copy link
Contributor Author

danvk commented 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:

interface Point {
  x: number;
  y: number;
}
type Axes = keyof Point;
// type is keyof Point

type AxesExpand = unknown & Axes;
// type is keyof Point

type AxesExpand2 = unknown & keyof Point;
// type is keyof Point

@RyanCavanaugh
Copy link
Member

image

@RyanCavanaugh
Copy link
Member

Or

type Q = Exclude<Axes, never>;

@danvk
Copy link
Contributor Author

danvk commented Jan 13, 2021

Thanks @RyanCavanaugh !

@ftzi
Copy link

ftzi commented Jan 16, 2021

Or

type Q = Exclude<Axes, never>;

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.

@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants