728x90
반응형
함수와 메소드에 type alias 지정
type funcType = (a: string) => number;
// 함수표현식
let func: funcType = function () {
return 10;
};
object 안에 함수 선언
let memberInfo = {
name: "kim",
plusOne(a: string): string {
return a;
}
}
let result = memberInfo.plusOne('plusOne');
함수 안에 함수 선언(callback함수)
- 위에 선언한 object안에 함수를 만드는 형태로 해봄
type funcType = (a: string) => number;
type funcStringtype = (a: string) => string;
let memberInfo = {
name: "kim",
plusOne(a: string): string {
return a;
},
cutZero(a: string): string {
if (a.startsWith("0")) {
a = a.substring(1);
}
return a;
},
removeDash(a: string): number {
return Number.parseInt(a.replaceAll("-", ""));
},
callbackFunc(a: string, b: funcStringtype, c: funcType): number {
let bResult = b(a);
return c(bResult);
},
};
let result = memberInfo.plusOne("plusOne");
console.log(result);
let cutZero = memberInfo.cutZero("0string");
console.log(cutZero);
let removeDash = memberInfo.removeDash("010-8236-6662");
console.log(removeDash);
let callbackFunc = memberInfo.callbackFunc(
"010-8236-6662",
memberInfo.cutZero,
memberInfo.removeDash
);
console.log(callbackFunc);
728x90
반응형
'Web > Typescript' 카테고리의 다른 글
[Typescript]Literal Type로 만드는 const 변수 유사품 (0) | 2022.06.11 |
---|---|
[Typescript]타입변수에 담기(type alias) & 타입 extend하기 (0) | 2022.06.11 |
[Typescript]타입을 미리 정하기 애매할 때 & 타입지정 & Narrowing (0) | 2022.06.11 |
[Typescript]컴파일시 세부설정(tsconfig.json) (0) | 2022.06.11 |
[Typescript]설치셋팅&기본문법 (0) | 2022.06.11 |