분류 전체보기

인터페이스(Interface) 개체(객체, 배열, 함수, 클래스 등)을 정의하는 타입 ? 나 readonly 사용 가능 (type , interface 둘다 가능) ? 속성 있어도 되고 없어도 되는 선택적 의미 interface User1 { name: string; age: number; isValid?: boolean; } const user: User1 = { name: "BAEK", age: 27, }; type UserT = { name: string; age: number; isValid?: boolean; }; const user: UserT = { name: "BAEK", age: 27, }; readonly 속성 읽기전용 속성을 만들 때 사용한다 interface User1 { name:..
타입 별칭(Alias) 새로운 타입 조합 생성 각각의 타입에 이름을 부여한 후에 원하는 곳에서 재사용할 수 있다 type MyTypeName = string | number; type MyArray = MyTypeName[]; // 별칭 사용 type UserA = { name: string; age: number; }; type UserB = { isValid: boolean; }; type UserX = UserA & UserB; const a: MyTypeName = "A"; // MyTypeName은 string 또는 number 할당 가능 const b: MyArray = [1, "A", "B", 2, 3]; // MyArray는 MyTypeName[]과 같으므로 배열 item으로 string ..
타입 가드(Guard)란? 타입 추론이 가능한 특정 범위(scople) 안에서 타입을 보장한다 instanceof const btn = document.querySelector("button"); if (btn instanceof HTMLButtonElement) { btn.classList.add("btn"); btn.id = "abc"; } const btn = document.querySelector("button"); if (btn) { btn.classList.add("btn"); btn.id = "abc"; } instanceof 를 사용하여 명시적으로 표현해도 되지만 null이 아닌 경우에만 동작하도록 표현해도 괜찮다 typeof function toTwoDecimals(val: number..
단언(Assertion)이란? 주저하지 아니하고 딱 잘라 말함 개발자가 타입스크립트에게 단언한다라고 말할 수 있다 타입 단언 단언 키워드 as Non-null 단언 연산자 - ! : null/undefined 데이터가 아님을 단언 ex1) const btn = document.querySelector("button") as HTMLButtonElement; btn.classList.add("btn"); btn.id = "abc"; const btn1 = document.querySelector("button")! btn1.classList.add("btn"); btn1.id = "abc"; const btn2 = document.querySelector("button") btn2!.classList.ad..