135

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?

1 Answer 1

152

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
};
Sign up to request clarification or add additional context in comments.

6 Comments

The simplest example is string literal types: type Easing = "ease-in" | "ease-out" | "ease-in-out";
I can't understand in depth. What's the difference between an interface and a type then? What is type majorly used for?
@KapilRaghuwanshi Microsoft thought someone could have been confused and wrote a paragraph about their difference. As they say, a rule of thumb might be "Prefer interface over type whenever you can".
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.