
type Add = (a:number, b:number) => number;
아래와 같이 사용할 수 있음.
const add:Add = (a,b) => a+b
type Add = {
(a:number, b:number) : number
(a:number, b:string) : number
}
const add:Add = (a,b) => {
if(type b === "string") retrun a
return a + b
}
//push의 다양한 기능
Route.push({
path: "/home",
state: 1
})
Router.push("/home")
type Config = {
path: string,
state: number
}
type Push = {
(config: Config): void,
(config: string): void
}
const push: Push = (config) => {
if (typeof config === "string") console.log(config);
else console.log(config.path);
}
//파라미터의 갯수가 달라도 된다
type Add ={
(a:number,b:number):number;
(a:number,b:number,c:number):number;
}
//a , b만 return 하고 싶을 때 c까지 확인하고 싶을 때 다양하게 사용하면 된다.
const add:Add=(a,b,c?:number)=>{
if(c) return a+b+c;
return a+b;
}
add(1,2)
add(1,2,3)
❌ type SuperPrint = {
(arr: number[]):void
(arr: boolean[]):void
(arr: string[]):void
(arr: (number | boolean)[]):void
}
🆗 type SuperPrint = {
<제네릭이름 보통 T or V>(arr: 제네릭이름[]):제네릭이름
} // 제네릭을 받는걸 알려준다.
const superPrint: SuperPrint = (arr) => {
arr.foreach(i => console.log(i))
}
superPrint([1,2,3,4])
superPrint([true, false])
superPrint(["a", "b"])
superPrint([1,2,true, false])