- Static Checking: ts detetcs errors in our code without running it
- Type Checking: find erroa, analyzes our code, available in development only, for development purposes
- Check out the ts docs
- Run
npm i typescript --save-dev
to install on this folder alone ornpm i -g typescript
to install globally - Run
tsc
to test if it works
- If you cannot install ts for any reason, use TypeScript Playground to try thing out or practice ts
- File with .ts ending
- To compile your ts code to js, run
tsc filename
-
Assigning a basic type to variable by add
:Type
after variable name -
type
need to be in all lowercaselet myVar: type = value; const myVariable: string = 'This Is Awesome!!!';
- All lowercase as well, just
number
andboolean
const myNum: number = 7;
const myBoolean: boolean = true;
- run
tsc
=> we will see error shown in the terminal
//Type Inference
let tvShow = 'Criminal mind';
tvShow = 'CSI';
tvShow = false; //warning error type
- When we don't annotate a variable's type AND we don't initialize it with a value, it has a type of "any"
- Allow you to use any types but it is sort of defeats the purpose of TS and types => use it sparingly
const movies = ['Titanic', 'CSI', 'Criminal Minds'];
let foundMovie;
for (let movie of movies) {
if (movie === 'CSI') {
foundMovie === 'CSI';
}
}
foundMovie();
foundMovie = 1; //foundMovie was inference as type any, in this case we wanna specifically asign type for foundMovie without using type inference
- set type for your function's param
- without setting type, your param's type will be set default as
any
function cal(num: number){
return num * num
}
cal(6) // good
cal("hello") // ts will complain
function sayHello(name: string){
console.log("Oh hello ${name}")
}
sayHello("Ann") //good
sayHello(777) // ts will complain
- We can also work with default params like so:
//set name = there if when called function greeting() but does not provide any param
function greeting(name: string = "there"){
console.log("Oh hello ${name}")
}
greeting() // "Oh hello there"
greeting("Ann") // "Oh hello Ann"
- We can specify the type returned by a function by annotate the return type
- Add return type right after params. The doMath() return type is
number
const doMath = (x:number, y:number): number => {}