TypeScript TYPES vs INTERFACES (Key Differences)

preview_player
Показать описание
TypeScript has two ways of declaring structures of your objects in the form of #types (type aliases) and #interfaces.

In this TypeScript tutorial we will look at the technical differences between these two, when you should use which, along with real world #TypeScript code analysis, and community thoughts 🌹.

🚥 Professional Courses

Additional Resources

👇 SUBSCRIBE for MORE 👇

**Feel Free To Read This Lot**

I'm Basarat, That TypeScript Guy and I love helping developers. More about me:

Microsoft MVP for TypeScript
150K contributions on Stackoverflow. Top Contributor for TypeScript.
Book Author : Beginning NodeJS
Book Author : TypeScript Deep Dive
Creator of multiple hot ✨ed Github Open Source projects

Рекомендации по теме
Комментарии
Автор

Would it be fair to say: Type first coding style feels more natural in functional programming and Interface first coding style fits more the object oriented programming style? - Anyways another eye opening lesson from a Typescript Guru! Thank's a lot for providing such high level content in public!!

hansschenker
Автор

A key difference, I believe, is that interfaces have type-system identity while 'type'-defined types are just aliases - the type name "disappears" once an instance of it is encountered and translated by the compiler. This can make a big difference in readability of compiler error messages and debugger output, which tilts me toward using interfaces for anything non-trivial where they work.

edstaub
Автор

My impression is that interfaces was more common before and types more common now? The codebases are (in TypeScript-terms) kinda old so it stands to reason that they lean more towards interfaces.

I guess another reason is that interfaces feels more natural in an OOP context (as you alluded to), while types feel more natural in other styles such as FP and procedural (again, as you kinda alluded to).

Personally I also lean towards type declerations as I like to only use on concept as I often use features that I need types for anyway (union types for example). Then again: I tend to avoid the classical (hah) OOP parts of JavaScript leaning both more on FP and procedural and for the few times I _do_ use classical OOP types can be used in the 'implements' part of classes anyway.

EDIT: Hah, I see other have commented similar things.

torb-no
Автор

oh, I just realize that 'class A implements B {...}' (where B is a type) is actually possible in TS!

gludion
Автор

Great content, thanks! 👍 I will come back to this video every time I forget the difference 😂

robdev
Автор

I cannot believe you don't have 1million+ subscribers

mmalam
Автор

Statistics on a real code base – awesome idea! Nice work, subscribe to your channel.

conw_y
Автор

I am an Interface Type of person and I even use an “I” in front of them (IFunctor, IApply, ISemigroupoid) despite the millions of people arguing why using ”I” is not the best approach... They will say I am lazy because I don’t have to find names for my Interface using the easy way and they are absolutely right (I also use MapType, ApType or ComposeType ... to name my types sometimes but I am a hobbyist, a lone wolf and not part of a team)

Luxcium
Автор

I have no idea why people are disliking a great video like this one, great job man 👍

nelyousfi
Автор

Awesome video 👍. Specially liked the last statistics touch.

For me, i mostly use interfaces for objects or more complex stuff, types for primitives or functions, which are mostly one off and don't have a hierarchy.

Thanks again for the video 😀

bendtherules
Автор

This was very helpful! Thanks for the great video!

giribhatnagar
Автор

Love your videos, and I am your new subscriber, please make more typescript videos about tips and tricks and Advance topic about typescript
can you tell me where can I learn Everything about typescript, all the Advance concepts, every topic?

alinawaz
Автор

Good approach to explanation. Thank you.

ffatheranderson
Автор

What's up with all the downvotes? Solid video from a TS expert!

GrubyCh
Автор

I enjoy your videos, the one slide overview was great, but also the statistical analysis of the different Microsoft codebases. Only complaint: Following the link to the source code doesn't work. Did you delete it on purpose?

floy
Автор

How to create drap down in type script

ramamadhavanagireddy
Автор

My dear, did u check your monetization, promotes vulgar netflix ads. By the way your knowledge of typescript is too deep.

xtremewolve