hackjutsu
hackjutsu

Reputation: 8932

What is `export type` in Typescript?

I notice the following syntax in Typescript.

export type feline = typeof cat;

As far as I know, type is not a built-in basic type, nor it is an interface or class. Actually it looks more like a syntax for aliasing, which however I can't find reference to verify my guess.

So what does the above statement mean?

Upvotes: 135

Views: 196489

Answers (1)

James Monger
James Monger

Reputation: 10685

This is a type alias - it's used to give another name to a type.

(Compare type vs interface here)

In your example, feline will be the type of whatever cat is.

Here's a more full fledged example:

interface Animal {
    legs: number;
}

const cat: Animal = { legs: 4 };

export type feline = typeof cat;

feline will be the type Animal, and you can use it as a type wherever you like.

const someFunc = (cat: feline) => {
    doSomething(cat.legs);
};

export simply exports it from the file. It's the same as doing this:

type feline = typeof cat;

export {
    feline
};

Upvotes: 151

Related Questions