Type alias

wang xiao bo 's photo
wang xiao bo
·Aug 31, 2021·

1 min read

Subscribe to my newsletter and never miss my upcoming articles

Type aliases are used to give a new name to a type.

Simple example:

type Name = string;
type NameResolver = () => string;
type NameOrResolver = Name | NameResolver;
function getName(n: NameOrResolver): Name {
    if (typeof n === 'string') {
        return n;
    } else {
        return n();
    }
}

In the above example, we use type to create type aliases. Type aliases are often used for union types.

 
Share this

Impressum

JavaScript Ruby on Rails