const el = document.getElementById("foo"); // type is HTMLElement | null
if (el) { el; // type is HTMLElement el.innerHTML = "party time".blink(); } else { el; // type is null alert("No element #foo"); }
7. in 연산자 사용
1 2 3 4 5 6 7 8 9 10 11 12 13 14
interface A { a: number; } interface B { b: number; } functionpickAB(ab: A | B) { if ("a"in ab) { ab; // type is A } else { ab; // type is B } ab; // type is A | B }