Advertisement
Intermediate TypeScript, Migrating JavaScript to TypeScript

How do you handle a third-party JavaScript dependency that has no available type definitions during migration?

Short answer

Either install a community @types package if one exists, or write a minimal ambient declaration yourself (a .d.ts file with `declare module 'library-name';`), which effectively types the whole module as 'any' — unblocking compilation while leaving room to add real types later.

Save TypeScript mock interview
Advertisement
AModel answer
Either install a community @types package if one exists, or write a minimal ambient declaration yourself (a .d.ts file with `declare module 'library-name';`), which effectively types the whole module as 'any' — unblocking compilation while leaving room to add real types later.
// untyped-lib.d.ts
declare module 'untyped-lib';

// usage.ts
import untypedLib from 'untyped-lib'; // typed as 'any', but no longer a compile error
Real-world example
Unblocking a migration when a legacy internal or niche third-party package has no available type definitions.
Likely follow-ups
  • What's the risk of leaving a dependency typed as 'any' indefinitely, rather than eventually writing real types for it?
Related topics