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

Provide a core types Type (result of typeof) #61427

Closed
npenin opened this issue Mar 15, 2025 · 5 comments
Closed

Provide a core types Type (result of typeof) #61427

npenin opened this issue Mar 15, 2025 · 5 comments
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript

Comments

@npenin
Copy link

npenin commented Mar 15, 2025

⚙ Compilation target

es2015

⚙ Library

none

Missing / Incorrect Definition

I could not find a type that provides the result of typeof. The workaround is

declare let a: unknown;
const b = typeof a;
type Types = typeof b;

Sample Code

export function polymorph(type: Types){
switch(type){
///case ...
}
}

Documentation Link

No response

@MartinJohns
Copy link
Contributor

@uhyo
Copy link
Contributor

uhyo commented Mar 15, 2025

It might be worth pointing out that any userland definition won't be forwards compatible if a new primitive ever gets added to the language. 😶‍🌫️

@guillaumebrunerie
Copy link

It might be worth pointing out that any userland definition won't be forwards compatible if a new primitive ever gets added to the language. 😶‍🌫️

How so? Isn't the workaround in the OP forward compatible? It's also highly unlikely new primitives will be added to the language any time soon anyway (note that the records and tuples proposal isn't suggesting to add new primitive types anymore).

@uhyo
Copy link
Contributor

uhyo commented Mar 16, 2025

How so? Isn't the workaround in the OP forward compatible?

That workaround requires runtime code (typeof a). This means that a pure type-level implementation (type TypeOfResult<T> = ...) is impossible if you want it to be forwards compatible. This also means that such an implementation can't be served as a library, which refutes some of the No New Utility Types rationales.

It's also highly unlikely new primitives will be added to the language any time soon anyway

Yup 😅 similarly Decimals aren't going to be a new primitive, although some people are arguing that non-primitive Decimals aren't as useful as primitive one.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Declined The issue was declined as something which matches the TypeScript vision labels Mar 17, 2025
@typescript-bot
Copy link
Collaborator

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

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Mar 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

6 participants