So get ready to resolve all your doubts you are having with TypeScript!
I am assuming that you are totally unaware of TypeScript. TypeScript, As name suggest it is a Type checking language!
//app.js let num = 3 console.log(typeof num) // this will print number num = "I am String" console.log(typeof num) // this will print string
If you are working with large scale web application, I am assuming you are using React!
Lots of Components, Lots of Props passing through Child Components, Helpers functions to make API calls and a lots of things are going on in that Project.
Sometimes you probably have ended up spending hours behind the undefined error, Silly mistakes (Datatype mismatch).
i.e In a React App, A Component must accept string value.
But this is totally wrong. A name must have string datatype.
So how do we proceed further?
Level up with TypeScript
Here TypeScript come into the Picture!
TypeScript is use to make enterprise level web applications, which is powered by Microsoft!
TypeScript allow you to narrow down the type of the variable.
// app.ts // Narrowed down type to string let name:string = "Darshan" // This will show you the error name = 123
We fully type our variable name to string.
If you are writing your code in VSCode editor this will probably show you the error! Something like you ** cannot assign number value to string.**
You don’t have to explicitly define type every time. TypeScript can implicitly define type based on right hand side value.
app.ts const name = "I am String" // This will still show you the error! name = 8923
The example you have seen above is inference type binding.
There are other type binding ways
You can read more in the documentation!
Things to remember while working with TypeScript!
- TypeScript is Compile time Language.
- You can also create your own Type!
- To narrowed down your props/state you need to know the particular type.
- You need to configure project before you start writing code in TypeScript.
So this is how TypeScript can speed up you development and save more time.
Ready to play with TypeScript? Checkout TypeScript playground!
Thank you for reading the blog. If you liked it, Let me know your thoughts in comment box!