[TS] 타입 추론 및 타입 정의 연습
TIL
1. 타입 추론let a = 10;const b = 20;const c = [1, 2];const d = [1, "hello", true];const e = [1, 2, 3] as const;type A = number;type B = number;type C = number[];type D = (number | string | boolean)[];type E = [1, 2, 3]; 2. 조건을 만족하는 타입 정의- Animal 타입은 Dog 타입일수도 Cat 타입일수도 있다.- DogCat 타입은 Dog이자 Cat이다.type Dog = { name: string; color: string; };type Cat = { name: string; age: number; };​​type Animal = Do..
[TS] 타입 추론
TIL
타입스크립트는 모든 변수에 일일이 타입을 정의하지 않아도 되는 편리함을 제공한다.그러나 모든 상황에서 타입을 잘 추론하는 것은 아니다.이렇게 타입 추론이 불가능한 변수에는 암묵적으로 any타입이 추론되는데, 만약 엄격한 타입 검사 모드라면 이러한 암시적 any타입의 추론을 오류로 판단하게 된다.타입 추론이 가능한 상황상황 1: 변수 선언일반적인 변수 선언의 경우 초기값을 기준으로 타입이 잘 추론된다.let a = 10;let b = "hello";let c = { id: 1, name: "devmark", profile: { nickname: "고견", }, urls: ["https://devmark.tistory.com/"],}; 상황 2: 구조 분해 할당객체와 배열을 구조 분해 할당하는..
[TS] 대수 타입
TIL
대수 타입이란 여러개의 타입을 합성해서 만드는 타입을 말한다.합집합 (Union) 타입let a:" string | number | boolean;a = 1;a = "hello";a = true;유니온 타입으로 배열 타입 정의하기let arr: (number | string | boolean)[] = [1, "hello", true];유니온 타입과 객체 타입type Dog = { name: string; color: string;};type Person = { name: string; language: string;};type Union1 = Dog | Person;let union1: Union1 = { // ✅ name: "", color: "",};let union2: Union1 = {..