You Should Be Using Types Instead Of Interfaces In TypeScript

preview_player
Показать описание


🌎 Find Me Here:

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

Conclusion. Use the right tool for the job. There are situations where interfaces make more sense and situations where types make more sense. However types are best as default unless you need specific features from the interfaces

ChrisVisserDev
Автор

The semantic behind interfaces is to describe an object behavior contract. It's an OOP concept. A class can implement several interfaces. It kinda solves the unique inheritance limitation and allows to write very generic code by expanding Liskov Substitution capabilities in OOP.

This why you should use interfaces. For everything else, use types. The right tool for the right job. Both have their purpose.

fabien-bouvier
Автор

Interfaces are for OOP, types are for making types. Don't mix and match both.

md.redwanhossain
Автор

Don't approach this as black and white, it's fine to use both. I personally use Interfaces for defining objects(ie. React component's props) and Types for pretty much everything else.

Saru-Dono
Автор

This is something like; don't wear sweater. wear shirt instead.

Автор

also interfaces lets you use the `this` keyword.
which is useful sometimes

nomadshiba
Автор

Interfaces can be extended. They are also technically not as heavy on the ts server

hugodsa
Автор

interface declaration merging goes hand in hand with module augmentation. some examples:
- defining custom matchers in jest
- correcting a type in an upstream package
- defining custom jsx elements
- convenient global types for modules - e.x. Express.User, which is the type of req.user in an express app. You can extend this type to add your own properties to req.user. This is similar to the jest example

IMO types are usually better but sometimes your problem can only be solved with an interface, and sometimes you are defining some type that classes shoulr conform to (a proper OOP "interface").

harleyspeedthrust
Автор

I use interfaces for functional components props, and type for the rest (rtkq api requests/responses, etc)

StefanoV
Автор

eslint would scream at seeing two interfaces with same name

agiamahprecious
Автор

Interface for object definitions, types for declaring object instance types.

EddieVillamor
Автор

I mainly use interfaces for defining objects but types for defining literals or tuples. They both have their uses :)

jackdavenport
Автор

To define React props which is better? interfaces or types? I think semantically interface fits better and since it's usually defined in the same file as the component definition there's less chance it will be redefined. For rest of the types in for example a types.ts file I think it's better to go with types.

HussainFazaal
Автор

I used to think types were the be-all and end-all for typescript. But after evaluating some blog articles in favor of interfaces, I now understand why the typescript team decided to put both in the TS ecosystem. They both have good use cases and we can't really say one is definitely better than the other. So, on this one, I'm going to have to disagree.

BeeBeeEight
Автор

strange that I recently found a page in the docs saying interfaces are more memory efficient when you use them correctly...

hri
Автор

Interface can use union as well. In fact typescript docs it self said that you should perfer interface over type since it give more info when you hover over the thing that use interface, I think typescript should merge the two to remove these kind of debate

HaiNguyen-cfji
Автор

Idk why it took so long for my eyes to read anything but “mamma1” 😂

dommy
Автор

You can do unions like that with interfaces, too. WTF are you talking about? The adding props thing is valid.

bloody_albatross
Автор

You can do this with inheritance though? Like have a parent interface of mammal and then have the person interface or animal interface inherit from mammal.

dylanthony
Автор

how do you use “extends” with interface?

alimodz