As we delve into the fascinating world of TypeScript, it becomes apparent that understanding and leveraging the nuances of type usage is pivotal to...
For further actions, you may consider blocking this person and/or reporting abuse
While I agree on when to use interface or the types, I feel that the title was a bit clickbaity ;-)
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 😉
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
withimplements
, 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.
I know the title sounds clickbaity but I had to teach them somehow. Also love the points you made about
types
Good oneI was expecting the same.
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...
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
.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.
Good work!
Here another great article regarding this topic I came across yesterday: totaltypescript.com/type-vs-interf...
The title doesn't reflect what's written here.
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
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.
ChatGPT
Clickbite title.
I know Brother, but they must now learn which is better for them
Nice!
Thanks 🙏
Clickbait title lol.
I came here seeing how interfaces was completely neglected. It appears it wasn't.
How on earth does this make sense?
Great explanation! You got me with the title.😄 Great execution.
I personally always use
type
since I code purely in the functionally programming paradigm so no classes in my projects .Nice article.
I learned a lot.
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.
What a misleading title, no doubt the quality of the platform has been decreasing with time because of such blogs!