Function Type
function 함수(a: string): number {
return 100;
}
- 함수 타입도 type alias 설정 가능.
- 단, 함수 표현식만 가능.
type 함수타입 = (a: string) => number;
const 함수2: 함수타입 = function (x) {
return 100;
};
const 함수2: 함수타입 = (x) => 100;
type 함수타입 = (a: string) => number;
const 함수2: 함수타입 = (x) => 100;
methods의 타입
type Member = {
name: string;
age?: number;
plusOne: (x: number) => number;
changeName: () => void;
};
const 회원정보: Member = {
name: 'kim',
plusOne(a) {
return a + 1;
},
changeName: () => {
console.log('안녕');
},
};
call back
type CutZeroType = (v: string) => string;
const cutZero: CutZeroType = (v) => (v[0] === '0' ? v.slice(1) : v);
type RemoveDashType = (v: string) => number;
const removeDash: RemoveDashType = (v) =>
Number(
v
.split('')
.filter((v) => v !== '-')
.join('')
);
const callBacks = (v: string, fn1: CutZeroType, fn2: RemoveDashType) => fn2(fn1(v));
console.log(callBacks('010-1111-2222', cutZero, removeDash));