I’ve been following Matt Pocock for a while and he started TypeScript tip tweets on Twitter a while ago. I’ve found them super informative and it’s what prompted this post. Here’s the latest one.
So wonderful devs, what are some things that folks might not be aware of about TypeScript? If you can link to a TypeScript Playground example or an example in your favourite online editor, even better!
Top comments (7)
One thing that I learned from the Ladybug Podcast is that it's a whole language that requires a compiler! I assumed it was just a library or a framework, but knowing that give weight to the consideration of adding it to a project.
I am not sure I would agree in defining TypeScript a whole new language. It is a superset of JavaScript and you can think of it as "extending" JavaScript.
If JavaScript is a truck, TypeScript is the trailer. It doesn't do much on its own.
Also, as Nikhil said, TypeScript doesn't reach the final user!
In React or Angular you can include it in devDependencies, for example.
I will checkout the Ladybug Podcast though!:)
Actually, the "overhead" will only exist during the development-time.
I wonder if people end up considering the same argument for adding
sass/scss
in their project.I was recently looking at some java code and liked the idea of method overloading signature.
Typescript too supports the same but, just at the type signature level.
Since, JS doesn't have native support for function/method overload.
Here is an example in playground
Not exactly a thing folks are not aware but, things I wish folks used more often:
{}
is not an empty object.Record<string, unknown>
is a better representation.Defining an interface for classes and usage of extends and implements (together).
Here is a link
Usage of Property accessor:
interface_name["property_name"]
Very often, I have noticed that we have a function that takes a specific property of an object as an argument. And we end up REPEATING the property's type in the function signature instead of ACCESSING the property's type from the object's interface.
Here is a playground link
Specifying a type for a constructor.
Links:
Why TS designed like this? Don't you think it's strange? A lot of scenes we can do just remember this? This assert is suitable for all scenarios, we don't know.
Can you elaborate? I’m not following your questions.
Yes, I find the example both meaningful and helpful. I only knew of type-guards as a way of doing this. It's a useful trick I would've loved to have discovered sooner.