FE/Typescipt

[Typescript] 함수

따봉치치 2023. 9. 1. 18:36
728x90

 

명시적 this

this 타입을 지정해 주지 않으면 자동으로 any가 됨

typescript에서는 any를 지양해야 함

 

interface Cat {
	name : string
	age : number
}

const cat : Cat ={
	name : 'kitty',
	age : 3
}

function hello(this: Cat, message : string) {
	console.log(`Hello ${this.name}, ${message}`)
}
hello.call(cat, 'You are sooooo cute!');

 

 

오버로딩

 

function add(a:string, b:string) : string //타입 선언
function add(a:number, b:number) : number //타입 선언

function add(a:any, b:any) { //함수 구현
	return a + b
}
728x90