author | tags |
---|---|
Gabriel Augusto (@gabrielolivrp) |
typescript programming types |
The TypeScript language has a very powerful type system, with it we can program in type level. An interesting fact is that its type system is turing complete, that is, it is possible to code any program that runs on the turing machine, the proof is here.
The example below is making use of conditional types to return the type of an animal's class.
// types of animals
type Dog = "Dog";
type Giraffe = "Giraffe";
// class of animals
type Mammal = "Mammal";
type Herbivore = "Herbivore";
// returns the class
type Class<A extends Dog | Giraffe> = A extends Giraffe ? Herbivore : Mammal;
// example
type E1 = Class<Giraffe>;
type E2 = Class<Dog>;