7.2 - Typescript
Why typescript





Why typescript

What is static typing?


Lets convert a small JS file to TS

JS
TS

How to add types


What about functions?
(Try it yourself - 5 minutes)

Lets write a calculator
(5 minutes)
1. Function has 3 arguments, first, second and type
2. type is a string which can be
a. "add", "div", "mul", "sub"
Problem ?
It's not strict enough

Solution
Union operator

Solution
Union operator


TS vs JS
How does JS run code
TS vs JS
How does TS run code? It never does
It does
1. Type checking
2. converts to JS


What is the TS compiler?
The compiler is called - tsc
Let's bring the counter code locally
npm i -g tsc
The tsconfig file
tsc --init

target

ECMAScript version
1. es3, es5
very old (if u want to support IE)
2. ES2016 - ES2020
incrementally better
3. esnext
Latest
module
module type - target system
1. none
This isn't a module
2. commonjs
Node.js code
3. amd
browser code
Lets try commonjs vs es6


forceConsistentCasingInFileNames
Does casing matter when importing modules
strict
How strictly should TS be evaluated
Constraints
Interfaces, types and enums

Interfaces
(Person interface)
1. Let you accumulate data of a specific type
2. Interfaces can use other interfaces
3. Interfaces can extend interfaces
4. Interfaces can be implemented by classes
Types
Very slightly different from interfaces
Need to be equated
Very useful for unions and ors
Can not be extended
Differences b/w types and interfaces
Can mostly be used interchangibly
1. Interfaces can be implemented by classes
2. Interfaces can extend each other
3. Types can do unions and intersections
Enums


7.2 - Typescript
By harkirat singh
7.2 - Typescript
- 109