TypeScript 개인 노트 학습
3116 단어 typescriptbeginners
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 {}
Reference
이 문제에 관하여(TypeScript 개인 노트 학습), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/pawandeore/learning-typescript-2cle텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)