pir
pir

Reputation: 5923

Include TypeScript types in TS export

I have a TypeScript library that I want to export for other JS/TS developers to use. As a benefit for users of my library, I'd like to export the types as well. How do I do that? I.e. how do I export a index.d.ts type declaration file?

I've looked at the TypeScript docs, but they seem to talk about writing explicit type declarations, whereas I want to automatically export type declarations when running npx tsc.

Here's the tsconfig.json:

{
  "compilerOptions": {
    "lib": ["ES2019"],
    "module": "es6",
    "target": "es5",
    "moduleResolution": "node",
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "sourceMap": true,
    "outDir": "build",
    "strict": true,
    "noImplicitAny": true,
  },
  "include": ["./**/*.ts"],
  "exclude": ["node_modules", ".vscode", "test"]
}

Upvotes: 1

Views: 52

Answers (1)

pir
pir

Reputation: 5923

Sometimes writing out a question tells you how to find the answer yourself :)

Adding --declaration as specified in the CLI docs does the trick, i.e.:

tsc index.js --declaration

Upvotes: 1

Related Questions