TypeScript is a modern programming language often preferred over JavaScript for its added type safety. In this article, I'll share the top 10 TypeS...
Some comments have been hidden by the post's author - find out more
For further actions, you may consider blocking this person and/or reporting abuse
Nice list, but this is wrong;
TypeScript is only a static type checker that checks at compile time (or rather transpilation time).
typeof
is a JavaScript feature, not a TypeScript feature. Even though javascript does not have strict static typing, every language must still have the concept of types at some point because hardware instructions are different for say string operations and numerical operations. Typescripts type system is something separate from that. It's ONLY a static checker performed before code is run, and TypeScript-specific code features will never run at runtime.Loved the explanation on dependency injection!
The covariance example is incorrect. Who else can spot it? I see many comments on the wonders of the article, many reactions too, but nobody notices it? Hmmm. People, what's up?
sorry! I just fixed it .
Still wrong.
Enlighten us Jose
The author changed the example completely now. This new example is OK, although it is a bit longer and has unneeded functions (for the purposes of demonstrating covariance).
console.log(func("True")) //Error: boolean cannot be passed as argument
is not correct. "True" is a string, but boolean is declared as acceptable in herefunction func(param:number|boolean)
. Comments lie. Am I an liar?I didn't get you
The string "true" is not a the boolean type true.
typeof("true") !== typeof(true)
I know I wanted to show that it we will get error when we pass "True" when we are only supposed to pass either string or number.
I think you refer the wrong part. I am talking about
6. Conditional Types
.Quoting:
**number **and **boolean **are allowed, but you pass a string. The comment is wrong telling a **boolean **can not be passed. Same for using
"True"
as string pretending a true boolean is very misleading and terrible wording.Ahhh!Let me fix the comment.
Thanks for reading!
@niharikaa you should fix the comment. It is "Error: string cannot be passed as argument. " Boolean is one of the optional parameters in the function. "True" is the string, it is not a boolean. true without quotes and without capital letter is a boolean
You're welcome and thanks for the article.
Thanks to your article, I was able to understand D.I
Very helpful!Thanks for the article.
Awesome detailed explanation 🔥
Amazing article!
Such a Great Article......Thanks for Sharing....!!!!!!
Great very useful all points.
Thanks for sharing ☺️
Thanks for sharing such a detailed blog!
great article and great explanation too👏
Cool!
Great Article!
Wouldn't example 3. implemented in the UserRepository class should be:
Since you've passed
number
|string
as type in the parameter ofgetById
method?Although it will be more longer, I think it is more appropriate to stick to the pattern,
Great article btw 😉.
getById(idOrName: number | string): User | undefined, works because it is structurally compatible with the interface. TypeScript ensures that the method can handle number as required by the interface. The additional support for string is treated as extra functionality, not a violation. TypeScript does not strictly enforce that methods must match the interface exactly, as long as they satisfy the required types. This flexibility allows the class to extend functionality while remaining compatible.
Yes, we can adjust it as you suggested for better clarity. However, the provided example is already working perfectly fine.
HasProperty was cool to know! But in what context I can use it?
Here's an example where I had to use it. I was using a component library in Nuxt.js for a table. It took in two paramters; data and columns. The columns parameter was a list of objects with a property
label
of type string and a propertykey
of type string. The key described which properties to read for that specific column. For example:It's a similar problem, but slightly more advanced because you need to create a recursive type as well as do some string literal types in typescripts type system (not javascript).
My brain hurts but its cool. something new to learn about typescript <3
Examples are often too lengthy and can be confusing, particularly those related to generics. The last example, "Dependency Injection," is not specifically a TypeScript concept. I was expecting to see advanced TypeScript concepts, as the title suggests, but it feels more like clickbait.
It would be interesting to go through some real-world examples or use cases where these advanced TypeScript concepts, like conditional types or dependency injection, have significantly improved the development process or project outcomes.
Man, was preparing for typescript interview today mrng, i asked gpt to give a few examples about generics, it gave the same examples with same parameters, types and explanation
AI content!
Thanks for your feedback.
Some examples might be AI-generated, but I customized a few of them for better understanding. I also referred to the official documentation for accuracy.
Nice list of features and examples. The infer concept seems helpful but I still don't understand it as there wasn't an explanation, but good enough for me to lookup more on my own. Thanks for sharing!
Great. tnx.
These technics are not "advanced" because all of them just what make Typescript - Typescript. Not the first day.
Every programming language has advanced concepts that you come across as you gain more experience. These concepts are, of course, expressed in the language itself, so I'm not sure what you're trying to point out here.
something new to learn about typescript <3
Good article. thank you