Skip to content
0:00 / 0:00

#typescript#types#interfaces

[DEPRECATED] TypeScript: Should you use Types or Interfaces?

Matt Pocock

429K subscribers • 9 videos on Hallway

YouTube
  • Recommended by 7 people: 7 in engineering communities.
  • Presented by Matt Pocock, TypeScript educator
  • May be out of date. The tools, versions or practices it shows have likely moved on.
  • An opinion. It argues one side of a question engineers disagree on.

Intermediate. Assumes working knowledge.

181K views3 years ago7 recommendations

Programming languagesTypeScript

Matt Pocock compares TypeScript types and interfaces, explaining why he prefers types due to issues like interface merging.

The advice in this video is outdated. See the updated version here: youtube.com/watch?v=e0AIkYrXAYE Become a TypeScript Wizard with Matt's upcoming TypeScript Course: totaltypescript.com/ Follow Matt on Twitter twitter.com/mattpocockuk

Why it's here

7 people recommend this

What people say. Often shared in discussions about the differences and preferences between types and interfaces in TypeScript.

u/devsmackin r/typescript • Recommends

On “Should I use type or interface? what's the difference?”

Historically, interfaces were the default because they were faster. That is no longer the case, interfaces also come with gotchas so I’d default to types. More details: link to this video

u/Hurinfanin r/typescript • Recommends

On “Interface vs Type”

I exclusively use types unless I need declaration merging. link to this video Bascially what he says

u/phischer_hin r/typescript • Recommends

On “Interface of Type.. New to TypeScript”

link to this video Types because they can do anything that Interfaces can, except for the dangerous stuff (merging)

u/tawieldenin r/typescript • Recommends

On “Should I use type or interface? what's the difference?”

Our team tend to use interface if we're exporting to allow extension and type for everything else. This is a useful resource - link to this video

u/SlimeBrowin r/typescript • Recommends

I did not know that about types, nor interfaces, lol. Which one do you prefer atm?

u/PooShamin r/typescript • Recommends

On “Should I use type or interface? what's the difference?”

Which one you should use is a hot topic. Type aliases can express more things, but interfaces can look nicer and are said to be faster for the type checker (although this has been a topic of debate lately). There are a few instances where you need interfaces to express certain types, but in general you should be able to use type aliases everywhere. Here's a good video on the topic

u/it-birdsin r/typescript • Recommends

On “8 Tricks to write clean code in TypeScript - CodingHelp.io”

Nice article. Here some thoughts from my side. ```Interface``` vs ```type``` I generally tend to only use ```type``` (here a good explanation why as video link to this video Because ```interface```s in TS come with the nasty feature interface merging. That can be helpful with globals, but generally you don't want it. ```enum```s It has gotten better with TS 5.0, but still not perfect. Here a nice source that outlines the problem and shows a.good alternative: link to this video

u/PooShamin r/typescript • Mentions

On “Defining ContentEditableElement with type vs. interface”

> Tiny bit more performant and it's framed in a way that it easier to understand. This is apparently not true anymore from what I've heard. It used to be though. > If you have a type alias that is the union between one type and an inline-type, then is it really a type alias? Or are you just explicitly listing the different properties of a subtype? I don't think there's a meaningful distinction. It's a type alias because you can use it in in place of writing it all out when it's used, so instead of writing: ``` let example: T & { x: string } = { /* T members */, x: "xValue"}; ``` We can write ``` type TX = T & { x: string }; let example: TX = { /* T members */, x: "xValue"}; ``` And t…

u/toddspottersin r/typescript • Mentions

On “where to store all the types i create ?”

There's a good explainer here: link to this video Basically, types behave more predictably, and some of the behaviors of interfaces can come back to bite you if you're not careful. By defaulting to types, you can opt in to the special properties of interfaces only when you need to, and IMO using types be default means that the intentional use of an interface stands out more and indicates that there's probably something unusual going on that needs to be accounted for. Still, a lot of people prefer interfaces for everything, and that's fine as long as you understand the tradeoffs you're making.