# Call Signatures
-> 함수 위에 마우스를 올렸을 때 보게 되는 것
- 해당 코드의 변수나 함수의 타입을 알려줌
- 함수를 구현하기 전에 타입을 만들 수 있고, 함수가 어떻게 작동하는지 서술해둘 수 있음
type Add = (a: number, b:number) => number;
const add:Add = (a,b) => a+b
# Overloading
-> 함수 이름은 같지만 매개변수 또는 반환값의 타입이 다른 함수
type Add2 = {
(a: number, b: number): number
(a: number, b: string): number
}
const add5: Add2 = (a, b) => {
if(typeof b === 'string') return a
return a + b
}
- Add2의 call signatures를 보면 b의 타입이 number 와 string 동시에 지정되어 있다.
[ EX 1 ] call signatrues에 파라미터 타입이 다른 경우
type Config = {
path: string,
state: object
}
type Push = {
(path:string): void
(config:Config): void
}
// Push 타입은 string과 object를 받을 수 있음
const push:Push = (config) => {
if(typeof config === "string") console.log("do something")
else {
console.log(config.path)
}
}
[ EX 2 ] call signatrues에 파라미터 개수가 다른 경우
type Add3 = {
(a: number, b: number): number
(a: number, b: number, c: number): number
}
// c는 옵션(선택사항)이라는 표시로 ? 사인을 넣어줌
const add6:Add3 = (a, b, c?: number) => {
return a + b
}
'TypeScript' 카테고리의 다른 글
[TS] Typescript #day2 (2) | 2024.12.18 |
---|---|
[TS] Typescript #day1 (2) | 2024.12.16 |
[TypeScript] 타입스크립트란? (2) | 2024.02.26 |
[Error] addCase cannot be called with two reducers for the same action type (3) | 2023.11.23 |