DEV Community

Cover image for Use Type, not Interface in TypeScript

Use Type, not Interface in TypeScript

Timi on August 05, 2023

As we delve into the fascinating world of TypeScript, it becomes apparent that understanding and leveraging the nuances of type usage is pivotal to...
Collapse
 
darkmavis1980 profile image
Alessio Michelini

While I agree on when to use interface or the types, I feel that the title was a bit clickbaity ;-)

Collapse
 
treasuredev_ profile image
Timi

I know, I don't want to be the one forcing everyone to use what they're not familiar with I want them to learn from each of them, and pick what suites them best 😉

Collapse
 
noblica profile image
Dušan Perković

Unless you need declaration merging, I don't really see a need to use interfaces?
Even in your class example, you can just as easily use a type with implements, it should work fine.

You can use types to define object shapes and class contracts just as well as interfaces.

I was hoping that the title of the post would reflect these differences a bit more. Instead this seems like more of an opinion piece.

Collapse
 
treasuredev_ profile image
Timi

I know the title sounds clickbaity but I had to teach them somehow. Also love the points you made about types Good one

Collapse
 
daguttt profile image
Daniel Gutierrez Muñoz

I was expecting the same.

Collapse
 
kgf profile image
Kenneth G. Franqueiro

Plenty of folks have already commented on the clickbait title, but I'd further note that the title pretty much directly contradicts TypeScript's own performance recommendations to use interfaces whenever possible.

github.com/microsoft/TypeScript/wi...

Collapse
 
rompeldunk profile image
Magnus Gule

Hmm. Good catch!

So basically when we using:

type Carnivore = {
meat: string;
}
type Cat = Pet & Carnivore {
color: string;
};

..we've already exceeded the general performance recommendations from MS of using type.

Collapse
 
kgf profile image
Kenneth G. Franqueiro

To make sure it's entirely clear: MS's recommendation is not to use type in this case, and instead express your example as an interface, which performs better, because interfaces are flattened and cached.

interface Cat extends Pet, Carnivore {
  color: string;
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
gktim profile image
gkTim

Good work!

Here another great article regarding this topic I came across yesterday: totaltypescript.com/type-vs-interf...

Collapse
 
alkadohs profile image
Alkado heneliko

The title doesn't reflect what's written here.

Collapse
 
steffennilsen profile image
Steffen Nilsen • Edited

Title suggest you should use type over interface, but the article fails to mention why the author case is making this case. I see superficial chatGTP like listings of the properties of type and interface before a conclusion is reached with no arguments for the title presented.

"By applying these best practices, you can ensure that your TypeScript projects are not only efficient but also outrank others in search engine results"

How using type or interface in my code is going to affect SEO is beyond me

Collapse
 
imamdev_ profile image
Imamuzzaki Abu Salam • Edited

Wow, your eye is good.
In my opinion, AI generated is actually OK. but, please review it first @treasuredev_ LOL 😂 It's misleading for someone who just believe it without checking the truthness of this information.

Collapse
 
aitchkhan profile image
Haroon Khan

ChatGPT

Collapse
 
daxtersky profile image
Miko

Clickbite title.

Collapse
 
treasuredev_ profile image
Timi

I know Brother, but they must now learn which is better for them

Collapse
 
harithzainudin profile image
Muhammad Harith Zainudin

Nice!

Collapse
 
treasuredev_ profile image
Timi

Thanks 🙏

Collapse
 
shameel profile image
Shameel Uddin

Clickbait title lol.
I came here seeing how interfaces was completely neglected. It appears it wasn't.

Collapse
 
barrymichaeldoyle profile image
Barry Michael Doyle

By applying these best practices, you can ensure that your TypeScript projects are not only efficient but also outrank others in search engine results.

How on earth does this make sense?

Collapse
 
jrfrazier profile image
WebDevQuest

Great explanation! You got me with the title.😄 Great execution.

Collapse
 
saidfatah profile image
ThePainter

I personally always use type since I code purely in the functionally programming paradigm so no classes in my projects .

Collapse
 
chideracode profile image
Chidera Humphrey

Nice article.

I learned a lot.

Collapse
 
dg_70ba62f1ee profile image
David Gottlieb

The reasons for one over the other are not compelling to use them as general rules. Especially the one saying if you need to share types across a project. Type doesn’t buy you anything additional.

Collapse
 
jimivi8846 profile image
Jimivi

What a misleading title, no doubt the quality of the platform has been decreasing with time because of such blogs!