TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Provide a core types Type (result of typeof)

Open npenin opened this issue 9 months ago β€’ 4 comments

βš™ 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

npenin avatar Mar 15 '25 07:03 npenin

This would fall under: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types

MartinJohns avatar Mar 15 '25 08:03 MartinJohns

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. πŸ˜Άβ€πŸŒ«οΈ

uhyo avatar Mar 15 '25 12:03 uhyo

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

guillaumebrunerie avatar Mar 16 '25 09:03 guillaumebrunerie

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.

uhyo avatar Mar 16 '25 10:03 uhyo

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

typescript-bot avatar Mar 20 '25 01:03 typescript-bot