TypeScript Interfaces vs Types

preview_player
Показать описание
I’m answering one of the most asked questions from my last video: What should I use? TypeScript Interface or Type?

It’s a great question, and one I greatly struggled with when I first started learning TypeScript.

Ultimately my advice is simple: use interfaces for objects, and types for everything else.

If you want the longer answer, check out the video.

——

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

"The short answer, and the one that let's you stop watching this video..." Bravo

zip
Автор

Using Types in Typescript favors for Functional Style Programming. You can compose complex types from simple types.
Using Interfaces tend towards an OOP Style. You define the shape of objects. You can mimic an object hierarchy with interface merging (intefaces with the same name merge into one)!
For me: Composition over Inheritance!

hansschenker
Автор

Best video on this. You gave the answer quickly and simply, but provided enough valuable info to keep me watching the rest

kevin.malone
Автор

I like how you talked about why to use either instead of just saying they exist. Very nice

caizza
Автор

three unique features that interfaces have:
1. It is more geared towards objects, classes and functions
2. It can extend
3. It supports declaration merging

imdad
Автор

Short answer. Thanks for being honest sir. Even though I watched the entire video.

LucioPoveda
Автор

Declaration Merging: The same name of Interface declarations can be do merging, while types cannot because it is fixed and static shape of object.
Types merging is by union and intersection
Interface is the better choice when you write a library to export because interface could be extended and change to fit the needs of library client.
If you use objects and classes, use interfaces as well. Otherwise use types.

hecalvin
Автор

My recommendation is to use types exclusively for the simple reason that everything can be typed with the type keyword, but not everything can be typed with the interface keyword. Additionally, if you need to modify an object type, you can do so with the TypeScript global generics e.g. Omit<ObjType, 'name' | 'age'>, Required<ObjType>, and Partial<ObjType>.

SogMosee
Автор

We can say interface is a contract that the implementing class, function or object must fulfil. But unlike interface types can’t be inherited. Nice video Harry . make a video on generics as well !!!

ManasTunga
Автор

Bro the best thing anyone has ever done, short answer first!

seanknowles
Автор

I really hope to master this Typescript someday

buka.a
Автор

Harry, thank you for this! I tend to be an interface person myself.... except when it comes to defining any kind of function, then it's straight to types. Absolutely agree with this video, haha! Great content as usual. :)

Digibeario
Автор

“I just had lunch... and it was delicious.” Hilariously said; actually did make me laugh out loud. Love your comfort level on camera... something I will now strive to obtain.

DedicatedManagers
Автор

It would be cool if when you typed the Interface name, there would be a snippet that auto completes the object and places the cursor on the fields. So at 3:10 typing Person there should be a snippet for “= {
name: $1,
hungry: $0
}

IkraamDev
Автор

Holy cow, what a deep voice you have!
For me listening to this video was like a flashback of me playing "Splinter Cell: Chaos Theory" a long time ago)

UkraineKiT
Автор

I like types for restricting values - type Thing = 'foo' | 'bar'

gosnooky
Автор

You explained very well. Finally I was able to understand Interface and type

surajpawar
Автор

I love the fact that you gave me an actual answer in the begining, so many people enjoy wasting time or fill space I guess. (Still watch to the end :))

ChrisPHolder
Автор

Thanks for the quality content. I got an answer to my question and became ur new subscriber

megaworld
Автор

Thank you, my friend. this was awsom

fatahbe