diff --git a/packages/svelte2tsx/src/emitDts.ts b/packages/svelte2tsx/src/emitDts.ts index fb312a36d..a53a9ce5d 100644 --- a/packages/svelte2tsx/src/emitDts.ts +++ b/packages/svelte2tsx/src/emitDts.ts @@ -111,9 +111,7 @@ function loadTsconfig(config: EmitDtsConfig, svelteMap: SvelteMap) { options: { ...options, noEmit: false, // Set to true in case of jsconfig, force false, else nothing is emitted - moduleResolution: - // NodeJS: up to 4.9, Node10: since 5.0 - (ts.ModuleResolutionKind as any).NodeJs ?? ts.ModuleResolutionKind.Node10, // Classic if not set, which gives wrong results + moduleResolution: ts.ModuleResolutionKind.Node16, declaration: true, // Needed for d.ts file generation emitDeclarationOnly: true, // We only want d.ts file generation declarationDir: config.declarationDir, // Where to put the declarations diff --git a/packages/svelte2tsx/test/emitDts/samples/cross-package-generic-types/README.md b/packages/svelte2tsx/test/emitDts/samples/cross-package-generic-types/README.md new file mode 100644 index 000000000..b50987e29 --- /dev/null +++ b/packages/svelte2tsx/test/emitDts/samples/cross-package-generic-types/README.md @@ -0,0 +1,22 @@ +# Cross-Package Generic Types Test + +This test validates that generic type information is correctly preserved when importing generic classes from external packages during `.d.ts` generation. + +## Issue +Before the fix, when using modern module resolution (bundler/node16), cross-package imports of generic classes would lose their type information and fall back to `any` in generated declaration files. + +## Test Structure +- `core-package/` - Mock external package containing `GenericToken` class +- `src/consumer.ts` - Consumes the generic class with type parameters +- `expected/` - Expected output showing preserved generic types + +## Key Test Case +```typescript +// This should preserve the generic type as GenericToken +// Before the fix: compiled as 'any' due to module resolution issues +// After the fix: correctly typed as GenericToken +export const SERVICE_TOKEN = new GenericToken('MyService'); +``` + +## Fix +Updated `emitDts.ts` to use modern module resolution (bundler/node16) instead of legacy node10 resolution. \ No newline at end of file diff --git a/packages/svelte2tsx/test/emitDts/samples/cross-package-generic-types/core-package/package.json b/packages/svelte2tsx/test/emitDts/samples/cross-package-generic-types/core-package/package.json new file mode 100644 index 000000000..c95846046 --- /dev/null +++ b/packages/svelte2tsx/test/emitDts/samples/cross-package-generic-types/core-package/package.json @@ -0,0 +1,12 @@ +{ + "name": "@test/core", + "version": "1.0.0", + "type": "module", + "exports": { + ".": { + "types": "./dist/index.d.ts", + "import": "./dist/index.js" + } + } +} + diff --git a/packages/svelte2tsx/test/emitDts/samples/cross-package-generic-types/expected/consumer.d.ts b/packages/svelte2tsx/test/emitDts/samples/cross-package-generic-types/expected/consumer.d.ts new file mode 100644 index 000000000..d3e52b350 --- /dev/null +++ b/packages/svelte2tsx/test/emitDts/samples/cross-package-generic-types/expected/consumer.d.ts @@ -0,0 +1,13 @@ +import { GenericToken } from '@test/core'; +export declare class MyService { + private name; + constructor(name: string); + getName(): string; +} +export declare const SERVICE_TOKEN: GenericToken; +export declare const ANNOTATED_TOKEN: GenericToken; +export declare const ASSERTION_TOKEN: GenericToken; +export declare class AnotherService { + value: number; +} +export declare const ANOTHER_TOKEN: GenericToken; diff --git a/packages/svelte2tsx/test/emitDts/samples/cross-package-generic-types/expected/index.d.ts b/packages/svelte2tsx/test/emitDts/samples/cross-package-generic-types/expected/index.d.ts new file mode 100644 index 000000000..7baccd565 --- /dev/null +++ b/packages/svelte2tsx/test/emitDts/samples/cross-package-generic-types/expected/index.d.ts @@ -0,0 +1 @@ +export * from './consumer.js'; diff --git a/packages/svelte2tsx/test/emitDts/samples/cross-package-generic-types/package.json b/packages/svelte2tsx/test/emitDts/samples/cross-package-generic-types/package.json new file mode 100644 index 000000000..385b6a9c4 --- /dev/null +++ b/packages/svelte2tsx/test/emitDts/samples/cross-package-generic-types/package.json @@ -0,0 +1,9 @@ +{ + "name": "test-cross-package-generics", + "version": "0.0.1", + "type": "module", + "dependencies": { + "@test/core": "1.0.0" + } +} + diff --git a/packages/svelte2tsx/test/emitDts/samples/cross-package-generic-types/src/consumer.ts b/packages/svelte2tsx/test/emitDts/samples/cross-package-generic-types/src/consumer.ts new file mode 100644 index 000000000..143c4b774 --- /dev/null +++ b/packages/svelte2tsx/test/emitDts/samples/cross-package-generic-types/src/consumer.ts @@ -0,0 +1,22 @@ +import { GenericToken } from '@test/core'; + +export class MyService { + constructor(private name: string) {} + getName() { return this.name; } +} + +// This should preserve the generic type as GenericToken +// Before the fix, this would be compiled as 'any' due to module resolution issues +export const SERVICE_TOKEN = new GenericToken('MyService'); + +// These explicit annotations should work regardless +export const ANNOTATED_TOKEN: GenericToken = new GenericToken('MyService'); +export const ASSERTION_TOKEN = new GenericToken('MyService') as GenericToken; + +// Test with different generic types +export class AnotherService { + value = 42; +} + +export const ANOTHER_TOKEN = new GenericToken('AnotherService'); + diff --git a/packages/svelte2tsx/test/emitDts/samples/cross-package-generic-types/src/index.ts b/packages/svelte2tsx/test/emitDts/samples/cross-package-generic-types/src/index.ts new file mode 100644 index 000000000..420f99b48 --- /dev/null +++ b/packages/svelte2tsx/test/emitDts/samples/cross-package-generic-types/src/index.ts @@ -0,0 +1,2 @@ +export * from './consumer.js'; + diff --git a/packages/svelte2tsx/test/emitDts/samples/cross-package-generic-types/tsconfig.json b/packages/svelte2tsx/test/emitDts/samples/cross-package-generic-types/tsconfig.json new file mode 100644 index 000000000..6052b7915 --- /dev/null +++ b/packages/svelte2tsx/test/emitDts/samples/cross-package-generic-types/tsconfig.json @@ -0,0 +1,14 @@ +{ + "compilerOptions": { + "target": "ES2022", + "lib": ["ES2022", "DOM"], + "module": "ESNext", + "declaration": true, + "strict": true, + "skipLibCheck": true, + "esModuleInterop": true, + "allowSyntheticDefaultImports": true + }, + "include": ["src/**/*"] +} +