How do you add TypeScript types for a library without bundled types?
This tests the DefinitelyTyped @types workflow. Install @types/package as a dev dependency via npm or yarn, skipping it when the library already bundles .d.ts files. A red flag is suggesting manual declarations or extra tooling before checking DefinitelyTyped.
WHAT THIS TESTS: This question checks if you understand the practical TypeScript ecosystem beyond the compiler. Specifically it probes whether you know about DefinitelyTyped, the community repository that publishes type declarations under the @types scope on npm, and whether you can distinguish between libraries that bundle their own types and those that do not. For a senior engineer, it also reveals if you know the exact installation workflow and can articulate when it is not needed.
A GOOD ANSWER COVERS: A strong response should hit four things in order. First, state that the standard process is to install the corresponding @types package from DefinitelyTyped as a development dependency, typically with a command like npm install --save-dev @types/lodash or yarn add --dev @types/lodash. Second, explain that once installed, TypeScript automatically picks up these declarations so you can import the library normally without extra configuration. Third, mention the important caveat that if the library already ships its own .d.ts files in the package, downloading the @types package is redundant and should be skipped. Fourth, note that no separate tools are required beyond your package manager, because TypeScript consumes @types packages automatically from node_modules.
COMMON WRONG ANSWERS: A major red flag is suggesting you need to manually write a custom .d.ts file from scratch for a popular library before checking whether @types already exists. Another weak answer is mentioning obsolete or unrelated tools like typings or tsd, which were used before @types became the standard. Candidates sometimes also incorrectly state that you must modify tsconfig.json to include the @types path or that you need to run a special type-acquisition command; neither is necessary because the compiler resolves @types packages automatically.
LIKELY FOLLOW-UPS: An interviewer might push deeper by asking what you would do if no @types package exists for a niche library. In that case, you would write a local declaration file, starting with a simple module declaration like declare module 'my-lib' { export function doThing(): void; }, and expand it as needed. They might also ask how you would verify whether a library already bundles its own types; the answer is to inspect the package.json for a types or typings field, or to look for .d.ts files inside node_modules/package-name.
ONE CONCRETE EXAMPLE: Suppose you want to use lodash in a TypeScript project. You first check node_modules/lodash/package.json and see no types field, confirming it does not ship declarations. You run npm install --save-dev @types/lodash. After installation, you can write import * as _ from 'lodash'; _.padStart('Hello', 10); and the compiler provides full autocomplete and type checking without any further steps.
Read the original → typescriptlang.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.