타입 추론 - 타입스크립트 이해하기

타입 추론

→ 초기 값의 변수들의 타입을 알아서 추론함 하지만 함수의 인자를 받을 땐 추론 안됨 이땐 타입 써야 함

let a = 10;
let b = "hello";
let c = {
    id : 1,
    name : "박정수",
    profile:{
        nickname: "GgoPpark",
    },
    urls: ["<https://winterlood.com>"],
};

let { id, name, profile, urls} = c;

let [one, two, three] = [1, "hello", true];

**// 함수의 반환값 타입도 자동으로 추론해줌**
function func(message = "hello"){ // 기본값이 설정된 매개변수는 추론됨
    return "hello";
}

**// any타입의 진화 (예외적인 사용이라 비추천 알아만 두라)**
let d;  // 이건 any 타입으로 추론됨 ( 암묵적any타입)
d = 10;
d.toFixed(); // 위에서 d = 10으로 넣어서 d는 또 number타입으로 추론됨

d = "hello"; 
d.toUpperCase(); // 이때는 또 string으로 추론됨

**// const로 선언한 변수는 해당 값의 리터럴 타입이 된다.**
const num = 10;
const str = "hello";

**// union배열 타입으로 추론해줌**
let arr = [1, "string"];