Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
93
rated 0 times [  95] [ 2]  / answers: 1 / hits: 42537  / 8 Years ago, fri, april 22, 2016, 12:00:00

What are the differences between the following?



type Foo = { 
foo: string
};
interface Foo {
foo: string;
}

More From » typescript

 Answers
11

Interfaces can be extended


interface A {
x: number;
}
interface B extends A {
y: string;
}

and also augmented


interface C {
m: boolean;
}
// ... later ...
interface C {
n: number;
}

Type aliases, however, can represent some things interfaces can't


type NumOrStr = number | string;
type NeatAndCool = Neat & Cool;
type JustSomeOtherName = SomeType;

So in general if you just have a plain object type, as shown in your question, an interface is usually a better approach. If you find yourself wanting to write something that can't be written as an interface, or want to just give something a different name, a type alias is better.


[#62444] Wednesday, April 20, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
elaine

Total Points: 628
Total Questions: 111
Total Answers: 104

Location: Marshall Islands
Member since Tue, Sep 21, 2021
3 Years ago
;