- 왜 타입스크랩트를 써야하나.
- TypeScipt 기초
- 함수 타입을 정의하자
- interface를 활용하자
- 타입 별칭이란?
- 연산자를 이용한 타입 정의 - 유니온 타입
- enum
- class
- generic
- promise Types
- todolist 만들어보기
- 디버깅
목차
함수에는 2가지 타입 정의 방식이 있다.
function functionType(a: string, b: number) {
return { a, b };
}
functionType("안녕", 123);
// ✅1. 함수에 타입 정의하기
function anyFunctionType(a: string, b: string) {
let testLetVariable = "테스트 let" + a;
const testConstvariable = 1 + b + 0;
return { testLetVariable, testConstvariable };
}
anyFunctionType("test let Type", "test const Type");
// ✅2. 함수에 반환 타입 정의하기
function anotherFunctionType(a: string, b: number): string {
return a + b;
}
anotherFunctionType("123", 123);
불필요한 인자를 전달하면 에러가난다
// 불필요한 인자를 넣으면 에러가난다.
function functionTypeRule(a: number, b: number): number {
return a + b;
}
// 💥 많을 경우
functionTypeRule(10, 20, 30, 40);
// 💥 적을 경우
functionTypeRule(10);
옵셔널 파라미터
// 옵셔널 파라미터 (함수의 선택적 파라미터)
function optionalType(a: string, b: string, c: number) {
return { a, b, c };
}
optionalType("string"); // 💥
optionalType("string", "123"); // 💥
optionalType("stirng", "string", 123); // ✅
// 음 반드시 입력을 안해도 괜찮긴 해 라는 느낌
function optionalTypeTest(a: string, b?: string, c?: number) {
return { a, b, c };
}
optionalTypeTest("string"); // ✅
optionalTypeTest("string", "string"); // ✅
optionalTypeTest("string", "string", 123); // ✅
테스트 하며 놀기
function testFunctionType(id: number, date: object, objectValue: object) {
const idTest = id + 1;
const curDate = date;
const objectValueTest = objectValue;
return {
id: idTest,
Date: curDate,
object: objectValueTest,
};
}
testFunctionType(123, new Date(), {});
function testAnotherTest(id: number, name: string, info: object): object {
return { id, name, info };
}
testAnotherTest(1, "1", { id: "jaeyoung" });