studye / typescript

타입스크립트는 자바스크립트랑 다른 언어인가요?
7 stars 0 forks source link

[Chapter2] Typescript가 기본적으로 타입을 예측하는 형태 #6

Open sculove opened 7 years ago

sculove commented 7 years ago

Inferred typing

타입스크립트는 처음 사용한 행태를 보고 타입을 추론한다.

var inferredString = "this is a string";
var inferredNumber = 1;
inferredString = inferredNumber; // compile error. type number' is not assignable to type 'string'.

Duck typing

Duck typing : 오리같이 보이고 오리같이 꽥꽥거리면 오리이다. 당연한 소리아냐? Inferred typing이 object에도 적용되는 것으로 보면 될것 같음.

var complextType = { name : "son", isOld: false };
complextType = { isOld: true, name: "song" }; // ok
complextType = { name: "byun" }; // 속성이 덜 되어있으면 fail
complextType = { isOld: false, name: "park",  national: "korea" }; // 속성이 더 추가되어도 fail