What is `export type` in Typescript?

JavascriptTypescript

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

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

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();
};

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

type feline = typeof cat;

export {
    feline
};

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhackjutsuView Question on Stackoverflow
Solution 1 - JavascriptJames MongerView Answer on Stackoverflow