DEV Community

Discussion on: The Difference between TypeScript Interfaces and Types

Collapse
 
balastrong profile image
Leonardo Montini • Edited

Hey, thanks for the article!
I've got a thing to mention on point 1 which reads "Interfaces are extendable - types are not".

You say:

If, after its been defined, I suddenly realise I want to add an address too, I will need to change the original type declaration. With interfaces, we can declare something new, and extend user

Actually, with types you can do pretty much the same using an Intersection Type with &.

type user = {
    name: string,
    age: number
}

type userWithAddress = user & {
    address: string
}
Enter fullscreen mode Exit fullscreen mode

The result is pretty much the same as with interfaces. The only difference is (as also explained in the doc) on conflicts, but as long as all fields are different, you can extend types with other types :)

Collapse
 
smpnjn profile image
Johnny Simpson • Edited

hey, that's a good point that I forgot about. Let me update the article. I've also updated to include the info on conflicts which is quite interesting (and something I didn't know about before, actually).