Static typing analyzes code at compile time against declared or inferred type constraints. TypeScript adds this system to JavaScript, catching errors early while improving readability, maintainability, autocompletion, navigation, and refactoring. section 3.0 section 3.6
In practical development and interviews, static typing supports:
- Detecting incorrect values and missing properties before runtime. section 3.6
- Making APIs and data structures clearer through interfaces, generics, type guards, and declaration files. section 3.12 section 3.9
- Supporting safer changes with type-aware autocompletion, navigation, and refactoring. section 3.6
- Enforcing stronger guarantees through type narrowing, assertions, strict compiler settings, and exhaustive checks. section 3.9
Third-party browser APIs
A React component loading the YouTube API can work at runtime while TypeScript reports that Window lacks YT and onYouTubeIframeAPIReady, and that an event parameter is implicitly any. Extending the global Window interface with declaration merging supplies those types and removes the errors. section 3.10
Listing 3.21 Declare the global YT namespace for window.YT and window.onYouTubeIframeAPIReady
declare global { #A
interface Window { #B
YT: { #C
Player: new ( #D
element: HTMLElement | null,
config: {
height: string; #E
width: string; #F
videoId: string; #G
events: {
onReady: (event: { target: { playVideo: () => void } }) => void; #H
};
playerVars: { #I
autoplay: number; #J
mute: number; #K
};
}
) => void;
};
onYouTubeIframeAPIReady?: () => void; #L
}
}
Inspect the declaration-merging example, including the typed YT player constructor, configuration fields, callback event, and optional global callback. section 3.10












