Door-check’s DOM code now has types the checker can trust. The rest of the code it relies on comes from outside src/. Vite runs the page, the export script uses Node.js’s own APIs, and Tobias Okonkwo wrote a helper for the theater’s seat map. Vite and the helper run as JavaScript, and Node.js’s APIs are built in, so where do their types come from?
For Node.js, the answer is the @types/node package you installed in the first lesson. It holds descriptions of Node.js’s APIs but no implementations.
In this lesson, you’ll find out where a package’s types come from, read a declaration file to learn what a package offers, and write a declaration for a package that has none. Start with Vite.
Find the Types a Package Ships
Door-check’s vite.config.ts calls defineConfig(), and the checker knows what that function accepts, even though Vite’s own code is JavaScript. In the first lesson, you met declaration files (.d.ts): files that describe types and hold no implementation code. Many packages ship their own, and the package’s package.json tells the checker where they are.
Open node_modules/vite/package.json and find its "exports":
node_modules/vite/package.json
"exports": {
".": "./dist/node/index.js",
"./client": {
"types": "./client.d.ts"
},
Read it from the bottom. The "./client" entry names its types under "types", and that’s the vite/client you loaded in src/vite-env.d.ts for import.meta.env. The "." entry is what import { defineConfig } from "vite" loads, and it names only a JavaScript file. In that case, the checker looks beside it for a .d.ts file with the same name, dist/node/index.d.ts. Other packages name their types in a top-level "types" field instead.
Vite’s declaration file has about 4,000 lines, and you don’t need to read it from the top. Open node_modules/vite/dist/node/index.d.ts in your editor and search for the name you use, defineConfig:
node_modules/vite/dist/node/index.d.ts
export declare function defineConfig(config: UserConfig): UserConfig;
export declare function defineConfig(config: Promise<UserConfig>): Promise<UserConfig>;
export declare function defineConfig(config: UserConfigFnObject): UserConfigFnObject;
export declare function defineConfig(config: UserConfigFnPromise): UserConfigFnPromise;
export declare function defineConfig(config: UserConfigFn): UserConfigFn;
export declare function defineConfig(config: UserConfigExport): UserConfigExport;
Each line is a declaration: declare tells the checker that a function with this signature exists somewhere else, so the line ends where a body would start. The code is in index.js.
Six declarations of one function are overloads, the ways you may call it. You can pass a config object, a promise of one, or a function that returns either. Door-check passes an object, so the first line applies. To learn which settings Vite accepts, search for UserConfig next.
Get Node.js’s Types From @types
Node.js’s built-in names, such as process and console, don’t come from an npm package at all, so you need a separate package for their types. They live in DefinitelyTyped, a community project that writes declaration files for code that has none and publishes each set on npm as @types/<name>. Its package for Node.js is @types/node, which you installed in the first lesson and listed in scripts/tsconfig.json: