What is TypeScript?
TypeScript is a typed superset of JavaScript that compiles to plain JavaScript which helps us build large scale applications with ease.
TypeScript is a typed superset of JavaScript that compiles to plain JavaScript which helps us build large scale applications with ease.
The main purpose of TS is to help developers be more productive by providing best in class tooling. (Eg. static type checking, editor intellisense, language features etc etc.)
Annotating your code isn't just about finding errors and validating the code before hand, it's also about documenting the code and conveying your intent more clearly to your future self and to other developers.
youtu.be
youtu.be
Getting Started.
Installation:
1. npm i -g typescript
2. create a project with `npm init -y`
3. run `tsc --init`
4. create an index.ts file
5. run `tsc` to build your project
`tsc --init` will create a tsconfig.json file for you with some default values
typescriptlang.org
Installation:
1. npm i -g typescript
2. create a project with `npm init -y`
3. run `tsc --init`
4. create an index.ts file
5. run `tsc` to build your project
`tsc --init` will create a tsconfig.json file for you with some default values
typescriptlang.org
Don't have time to setup npm & all that?
No worries you can proceed with online typescript playground:
typescriptlang.org
No worries you can proceed with online typescript playground:
typescriptlang.org
Typescript is smart enough to automatically infer type of a variable in most cases, and It is recommended that you let the compiler infer as much types as possible. Good Read:
fettblog.eu
Let's rewrite the above example:
fettblog.eu
Let's rewrite the above example:
The solution is type narrowing, Narrowing occurs when TS can deduce a more specific type for a value by analyzing the structure of the code, this is called "Control flow analysis"
Good Read: mariusschulz.com
let's see an example:
Good Read: mariusschulz.com
let's see an example:
<--- Type Aliases! --->
You can think of type aliases like variables in type level, but they do more than that. You can extract out, reuse & and compose types too.
Curious about Type Aliases vs Interfaces? here's a great thread by @_kamlesh_
You can think of type aliases like variables in type level, but they do more than that. You can extract out, reuse & and compose types too.
Curious about Type Aliases vs Interfaces? here's a great thread by @_kamlesh_
<--- Thank You! --->
Did I missed anything?
Any questions for me?
Any corrections?
I hope you learned some TypeScript & enjoyed the thread. :)
Did I missed anything?
Any questions for me?
Any corrections?
I hope you learned some TypeScript & enjoyed the thread. :)
CORRECTION 0: (<--- Objects! --->)
This example is incorrect, here's a revised example
Playground link: #code/JYOwLgpgTgZghgYwgAgArQM4HsTIN4BQyyMwUGYAXMhVKAOYDcRyANnBQPzW0OPIB6AcgA8AWjHIABpynJQAE2AI4kDMiwAHMMBxxWJYBFYKWcehGogArgFsARtGYBfAm4Q4KyODajnq6OQ4yAC8+IbkVMgARACCvubRADRsHFHRABJwAF5+yd4W1ABMAAzIzvxCGgDWBB4gXgBWWAAWIAGYwWF4ERTUcQn0+eaWyKXllcJYtfVNwLYdQbjdvekAUvPRE4JTtVXEyAB6x2hQWtBgAJ7IAOQjN-LqtsAYGAzyuFeaKDc9pJE8MB0EBMcoPezWMDIKAQACO1jIEAUH2QXx+gWwIBuADo3EA" target="_blank" rel="noopener" onclick="event.stopPropagation()">typescriptlang.org
This example is incorrect, here's a revised example
Playground link: #code/JYOwLgpgTgZghgYwgAgArQM4HsTIN4BQyyMwUGYAXMhVKAOYDcRyANnBQPzW0OPIB6AcgA8AWjHIABpynJQAE2AI4kDMiwAHMMBxxWJYBFYKWcehGogArgFsARtGYBfAm4Q4KyODajnq6OQ4yAC8+IbkVMgARACCvubRADRsHFHRABJwAF5+yd4W1ABMAAzIzvxCGgDWBB4gXgBWWAAWIAGYwWF4ERTUcQn0+eaWyKXllcJYtfVNwLYdQbjdvekAUvPRE4JTtVXEyAB6x2hQWtBgAJ7IAOQjN-LqtsAYGAzyuFeaKDc9pJE8MB0EBMcoPezWMDIKAQACO1jIEAUH2QXx+gWwIBuADo3EA" target="_blank" rel="noopener" onclick="event.stopPropagation()">typescriptlang.org
Loading suggestions...