Generated Declarations
Emitting .d.ts files from TypeScript and generating client types from OpenAPI or GraphQL schemas.
For packages and APIs you own, generate declarations from the source of truth instead of maintaining a hand-written second copy.
Set declaration: true when publishing TypeScript packages so consumers receive .d.ts files.
{
"compilerOptions": {
"declaration": true,
"declarationMap": true,
"declarationDir": "dist/types",
"outDir": "dist"
}
}For API clients, generate types from the OpenAPI or GraphQL schema and import those generated types.
OUTPUT
npx openapi-typescript openapi.yaml -o src/types/api.d.ts
npx graphql-codegen --config codegen.ymlimport type { components } from "@/types/api";
type User = components["schemas"]["User"];
type CreateUserRequest = components["schemas"]["CreateUserRequest"];In production
Handwritten API declarations drift. Generate from OpenAPI or GraphQL so the contract has one source of truth, then let CI typecheck consumers against the generated file.
Enjoyed this? Get more software craft delivered to your inbox.