egoist / egoist/tsup

values wrongly initialize in dts when importing json

Open
#1,012 1 comment 1 reaction 0 assignees View on GitHub
Dominant language
TypeScript
Stars
11.3k
Forks
275
PR merge metrics
No merged PRs in 30d

Description

having the following config:
```ts
import { defineConfig } from 'tsup';

export default defineConfig({
clean: true,
dts: true,
entry: ['src/index.ts'],
format: ['esm', 'cjs'],
platform: 'node',
sourcemap: true,
target: 'es2022'
});
```

and this (example) typescript code:
```ts
export { default as whatever } from './whatever.json';

export function something(): boolean {
return true;
}
```

tsup adds the following to the declaration files when exporting a json file in one of the ts files:

```ts
var whatever = [
"whatever json values the file has"
];

// other dts values (which are correct)
declare function something(): boolean;

export { something, whatever }
```

this makes typescript throw `Initializers are not allowed in ambient contexts. ts(1039)`

the solution would be to actually resolve the json type (just like how plain typescript does under `resolveJsonModule`) and type it as such

for example:
```ts
declare const whatever: string[];

declare function something(): boolean;

export { something, whatever }
```

another solution would be to just use valid dts syntax, this would be easier to implement as tsup wouldn't have to resolve the json type, but it would increase filesize
```ts
declare const whatever: [
"whatever json values the file has"
];

declare function something(): boolean;

export { something, whatever }
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.