pizzarob
pizzarob

Reputation: 12039

.d.ts file not included for nested file exporting types

I am using webpack and awesome-typescript-loader to bundle my project and create .d.ts files. I have a structure like this:

src
  - main.ts
  - types
    - index.ts
    - SomeType.ts

in main.ts I am exporting everything:

export * from './types';
export default ...

and types/index.ts looks like:

export * from './SomeType.ts';

Everything in the types folder is missing when I build the project, but I would expect .d.ts files to be created so I can import those types in other projects.

My tsconfig:

{
  "compilerOptions": {
    "removeComments": true,
    "moduleResolution": "node",
    "preserveConstEnums": true,
    "declaration": true,
    "sourceMap": true,
    "outDir": "./dist/",
    "noImplicitAny": true,
    "module": "esnext",
    "target": "es5",
    "lib": ["es2018", "dom"]
  },
  "include": ["src/**/*"],
  "awesomeTypescriptLoaderOptions": {
    "reportFiles": ["src/**/*.{ts,tsx}"]
  }
}

Upvotes: 0

Views: 2017

Answers (1)

ford04
ford04

Reputation: 74510

awesome-typescript-loader seems to behave differently from ts-loader and tsc regarding the emission of declarations from ts files with only contained type declarations.

Given a file types.ts with only a type export

export type MyType = "a"

and an import in another module index.ts

import { MyType } from "./types";

tsc and ts-loader both emit a types.d.ts file containing the type, but not awesome-typescript-loader. I cannot tell you the exact reason here - the only reference I found is that awesome-ts-loaders seemingly tries to be more intelligent based on more sophisticated dependency resolution approach, which could backfire in this edge case. When in doubt, I would assume tsc to have the correct reference behavior.

Anyway, it is more idiomatic to rename ts files with only type declarations to d.ts extension, as no code output is emitted. So you end up with the question, why d.ts files from a source directory are not copied to a build folder by tsc.

The easiest way for you in this case would be to manually copy the needed d.ts files (for a public library API or similar) to dist via a manual build step. Or you can use the new babel typescript preset with Webpack and have a separate tsc compilation step only for declarations.

Upvotes: 1

Related Questions