📎아이템 6 편집기를 사용하여 타입 시스템 탐색하기
📍 타입스크립트를 설치하면, 다음 두 가지를 실행할 수 있다.
언어 서비스


함수의 반환 타입을 number로 추론하였다.


객체에서는 개별 속성을 살펴봄으로써 타입스크립트가 어떻게 각각의 속성을 추론하는지 살펴볼 수 있다.


📍요약
Last updated






Last updated
function getElement(elOrId: string | HTMLElement | null): HTMLElement {
if (typeof elOrId === 'object') {
return elOrId;
// ~~~ Type 'HTMLElement | null' is not assignable to type 'HTMLElement'
} else if (elOrId === null) {
return document.body;
}
elOrId
// ^? (parameter) elOrId: string
return document.getElementById(elOrId);
// ~~~ Type 'HTMLElement | null' is not assignable to type 'HTMLElement'
}function getElement(elOrId: string|HTMLElement|null): HTMLElement {
if (elOrId === null) {
return document.body;
} else if (typeof elOrId === 'object') {
return elOrId;
// ^? (parameter) elOrId: HTMLElement
}
const el = document.getElementById(elOrId);
// ^? (parameter) elOrId: string
if (!el) {
throw new Error(`No such element ${elOrId}`);
}
return el;
// ^? const el: HTMLElement
}declare function fetch(
input: RequestInfo | URL, init?: RequestInit
): Promise<Response>;type RequestInfo = Request | string;declare var Request: {
prototype: Request;
new(input: RequestInfo | URL, init?: RequestInit | undefined): Request;
};interface RequestInit {
body?: BodyInit | null;
cache?: RequestCache;
credentials?: RequestCredentials;
headers?: HeadersInit;
// ...
}