egoist / egoist/tsup

Declaration files don't contain triple slash types directives

Open
#977 3 comments 4 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
11.3k
Forks
275
PR merge metrics
No merged PRs in 30d

Description

We are using tsup in storybook, but we seem to have a problem with the declaration files emitted by tsup.

This is a minimal reproduction of the file:

```ts
import { default as expectPatched } from '@storybook/expect';

export interface Expect extends Pick {
(actual: T): jest.JestMatchersShape<
jest.Matchers, T>,
jest.Matchers, T>
>;
}

export const expect: Expect = expectPatched as Expect;
```

When I just try to generate type declaration with tsc, it will output this:
```ts
///
export interface Expect extends Pick {
(actual: T): jest.JestMatchersShape, T>, jest.Matchers, T>>;
}
export declare const expect: Expect;
```

But when I do same with tsup, the triple slash types directive is missing:
```ts
interface Expect extends Pick {
(actual: T): jest.JestMatchersShape, T>, jest.Matchers, T>>;
}
declare const expect: Expect;

export { Expect, expect };
```

Can I somehow configure `tsup` to generate those directives. It seems like this is meant to be included:

https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html#-reference-types-
> For declaration files generated during compilation, the compiler will automatically add `/// ` for you; A `/// ` in a generated declaration file is added if and only if the resulting file uses any declarations from the referenced package.

This also breaks when building storybook in angular projects. When I manually add the directive it works again:
```ts
> yarn build-storybook --quiet
info => Cleaning outputDir: /storybook-static
info => Loading presets
info => Building manager..
info => Manager built (369 ms)
info => Compiling preview..
info => Copying static files: /tmp/storybook/sandbox/angular-cli-default-ts/node_modules/@storybook/manager/static at /tmp/storybook/sandbox/angular-cli-default-ts/storybook-static/sb-common-assets
info Addon-docs: using MDX2
info => Using implicit CSS loaders
info => Using angular browser target options from "angular-latest:build"
info => Using angular project with "tsConfig:/tmp/storybook/sandbox/angular-cli-default-ts/.storybook/tsconfig.json"
info => Using default Webpack5 setup
ERR! => Failed to build the preview
ERR! node_modules/@storybook/jest/dist/index.d.ts:8:31 - error TS2503: Cannot find namespace 'jest'.
ERR!
ERR! 8 interface Expect extends Pick {
ERR! ~~~~
ERR!
ERR! node_modules/@storybook/jest/dist/index.d.ts:8:50 - error TS2503: Cannot find namespace 'jest'.
ERR!
ERR! 8 interface Expect extends Pick {
ERR! ~~~~
ERR!
ERR! node_modules/@storybook/jest/dist/index.d.ts:15:27 - error TS2503: Cannot find namespace 'jest'.
ERR!
ERR! 15 (actual: T): jest.JestMatchersShape, T>, jest.Matchers, T>>;
ERR! ~~~~
ERR!
ERR! node_modules/@storybook/jest/dist/index.d.ts:15:50 - error TS2503: Cannot find namespace 'jest'.
ERR!
ERR! 15 (actual: T): jest.JestMatchersShape, T>, jest.Matchers, T>>;
ERR! ~~~~
ERR!
ERR! node_modules/@storybook/jest/dist/index.d.ts:15:83 - error TS2503: Cannot find namespace 'jest'.
ERR!
ERR! 15 (actual: T): jest.JestMatchersShape, T>, jest.Matchers, T>>;
ERR!
```

One workaround I found is setting the banner:
```ts
dts: {
entry: ['./src/index.ts'],
resolve: true,
banner: '/// ',
},
```

Contributor guide

Open the contributing guide

Research direction

Start with the minimal TypeScript example and the tsup dts configuration shown in the issue, comparing its emitted declaration with the output from tsc. Reproduce the missing jest triple-slash directive and verify that the generated declaration makes the referenced namespace available without requiring the banner workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
build-system
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.