let str: string = 'pawan';
let no:number = 30
let flag: boolean = true;
let d: null = null;
let e: undefined = undefined;
console.log(no)
console.log(str)
// Array
const cars:string[] = ['a','b','c']
const nums:number[] = [1,2,3,4]
const bool: boolean[] = [true, false];
function printCar(models : string[]){
models.forEach(element => {
console.log(element)
});
}
printCar(cars)
function callIt(model: number | string){
console.log(model)
}
callIt('true')
// TYPE
type mo = number | string
function printtCar(model: mo){
console.log(model)
}
printtCar(2)
// Function Type
function pr(cb: Function){
cb(20,'bmw')
}
type voo = number | string
pr((no : number, str : String )=> console.log(no, str))
// Interfaces
// large scale applications
type Car = {
make: string,
model : string | number,
sedan: boolean
}
interface Carr {
make: string;
model : string | number;
sedan: boolean;
//functions
accelerate : () => void;
decellarate() : void;
}
// Objects
function voe(obj_name: {param_a?: string, param_b : number}):void{
console.log(obj_name.param_b)
}
// interface Bv {
// nm: string
// }
// interface b2 extends By{
// nm2: number
// }
// Special Types
let car:any = 'bmw'
car = 'honda'
car = true
car = 22
// unknown
// let a: unknown = document.getElementById('my-element');
// we can throw what ever we want with unknow type
// void
// function returns nothing
// never
function crash(model:number): never{
throw new Error('car crash')
}
// it doesn't never returns values / anything not even void maybe kill process | terminate thing
// Literal types ( if declared with const )
type Bike = 'NOO' | 123
// const bike: Bike = 'wha';
const bike: Bike = 'NOO';
// Literal interface
const care = {
make: "honda" as "honda"
}
// care.make = 'mbw'
care.make = 'honda'
// Tuples
// only last paramater optional allowed
type CARRR = [number | boolean,string?];
// make readonly
type CARRR2 = readonly [number | boolean,string?];
// OR
let cDetails = [2021, 'bmw'] as const;
// cDetails[1] = 'akj';
const ar: CARRR = [1,'2']
console.log('tuples, -> '+ ar)
// maybe use tuple for API calls
// Rest Parameter
type carTuple = [number,string, ...number[]]
const car3 = [2021, 'bmw',23,123,34];
console.log(car3)
// IMPORTANT
// TYPE ASSERTATION explicility mentioning when don't know
const myCanvar = document.getElementById('canvas') as HTMLCanvasElement;
// NULL ASSERTATION when you don't have confirmation but sure about values types use -> !
function doSome(model: number | null){
model!.toString()
}
doSome(null)
// IMPOSSIBLE ASSERTATION
type YO = string | null
const car1 = 'form' as YO;
// IMPORTANT
// export {}
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)