Jean Lambert
Jean Lambert

Reputation: 141

Include types from external package to ts output

I have the following code:

import { ExampleType } from "@org/package-1
type AnotherType = ExampleType & number

I want ExampleType to also be included in the types definition output generated by tsc command line tool.

The output should look like this:

// dist/types.d.ts
type ExampleType = string | boolean
type AnotherType = ExampleType & number

I'm using typescript v4.6.4 and is how my tsconfig.json looks like:

{
  "include": [
    "./src/types",
    "./src/exceptions",
    "./src/config",
    "./src/index.ts",
    "./src/Client.tsx",
    "./src/Whatever.ts",
    "./node_modules/@org/package1",
    "./node_modules/@org/package2"
  ],
  "exclude": [],
  "compilerOptions": {
    "types": ["react", "react/jsx-runtime"],
    "jsx": "react-jsx",
    "baseUrl": "./src",
    "target": "ES6",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "module": "ESNext",
    "allowSyntheticDefaultImports": true,
    "moduleResolution": "node",
    "noImplicitAny": false,
    "lib": [
      "ES2017",
      "DOM",
      "DOM.Iterable"
    ],
    "allowJs": true,
    "declarationDir": "./dist/types",
    "resolveJsonModule": true,
    "emitDeclarationOnly": true,
    "declaration": true,
    "skipLibCheck": true,
    "stripInternal": true,
    "noResolve": true
  },
}

Upvotes: 0

Views: 1101

Answers (1)

Alex Wayne
Alex Wayne

Reputation: 186984

A module should export the types it wants to expose. It doesn't matter where those types come from. It may be constructed internally by a module, or imported from another module.

There's nothing special about that. Just import the types your module needs, and export the types you want your module to expose.

For example:

import { ExampleType } from "@org/package-1" // use in this file

export { ExampleType } from "@org/package-1" // export a type from another module
export type AnotherType = ExampleType & number // use an imported type in an exported type

export type YetAnotherType = { abc: number } // export a totally internal type

Upvotes: 1

Related Questions