DEV Community

Discussion on: The Type of a `type` in Typescript

Collapse
 
macsikora profile image
Pragmatic Maciej • Edited

The answer is that we can never ask about the types of types in TypeScript.

You can ask if type has a type(kind) at the type level also. You can do that by conditional type:

type IsKindOfString<T> = T extends string ? true : false
type ResultTrue =  IsKindOfString<"Hi I am type, don't confuse me with value"> // true
type ResultFalse = IsKindOfString<1> // false

Playground

Also my latest tweet about confusion in Type level and Value level operators:

Collapse
 
maxheiber profile image
Max Heiber • Edited

Thanks for your comment!
Your example using conditional types shows that we can ask TypeScript questions about types.
But one question we cannot ask is what the type of a type is.

This is a translation of your example from types to sets (in the sense of "set theory"). The TLDR is:

  • typeof is like "memberOf" in set theory

  • extends is like subsetOf in set theory

  • TS's type theory is like a set theory where sets cannot be members of other sets. But they are allowed to be subsets of other sets.

"foo" // a value
Set{"foo"} // the singleton set containing the value "foo"
"foo" memberOf Set{"foo"} // true statement
Set{ the infinitude of strings } // set containing "foo", "blah blah blah", the Gettysburg address, etc.

Set{"foo"} subsetOf Set{ the infinitude of strings }   // true statement

Set{"foo"} memberOf  SetX // this is the kind of thing TS won't let us ask