Type Checking Imported Modules
You'll learn how @ts-check validates types across module boundaries when libraries include .d.ts declaration files.
How Type Checking Crosses File Borders
When you enable @ts-check in a JavaScript file, the type checker doesn't just examine that file in isolation—it also looks at the modules you import. If an imported library provides TypeScript declaration files (.d.ts files), @ts-check reads those type definitions and enforces them at your import sites.
Think of .d.ts files as instruction manuals shipped with a product. When you import a library like lodash or express, the type checker consults the manual (the .d.ts file) to understand what types each function expects and returns. If you call an imported function incorrectly—wrong number of arguments, wrong types—@ts-check catches it.
What Happens in Practice
// @ts-check
import lodash from 'lodash';
// If lodash's .d.ts says chunk(array, size), the checker knows:
const result = lodash.chunk([1, 2, 3], 2); // ✓ correct
// This triggers an error because second arg must be a number:
const bad = lodash.chunk([1, 2, 3], "two"); // ✗ type error
If a library doesn't provide .d.ts files, @ts-check treats its exports as any, meaning no type checking occurs for that module. Many popular libraries include declarations; others can be supplemented with @types/ packages from npm (e.g., @types/node).
Key Benefits
This cross-module validation gives you compile-time safety for third-party code without writing TypeScript. You catch integration bugs early, right in your editor.
Key Takeaway:
@ts-check leverages .d.ts declaration files from imported modules to validate types across boundaries, catching misuse of library APIs before runtime.