values wrongly initialize in dts when importing json
- 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
Assessment
This issue has not been assessed yet.