Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/witty-kiwis-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte2tsx': patch
---

fix: respect moduleResolution setting in emitDts
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ bower_components
build/Release

# Dependency directories
node_modules/
/node_modules/
jspm_packages/

# TypeScript v1 declaration files
Expand Down
3 changes: 2 additions & 1 deletion packages/svelte2tsx/.gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.DS_Store
node_modules
/node_modules
/test/emitDts/samples/cross-package-generic-types/node_modules/.svelte2tsx-language-server-files
/index.js
/index.js.map
/index.mjs
Expand Down
7 changes: 5 additions & 2 deletions packages/svelte2tsx/src/emitDts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,11 @@ function loadTsconfig(config: EmitDtsConfig, svelteMap: SvelteMap) {
...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
options.moduleResolution &&
options.moduleResolution !== ts.ModuleResolutionKind.Classic
? options.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
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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { GenericToken } from 'test-package';
export declare class MyService {
private name;
constructor(name: string);
getName(): string;
}
export declare const SERVICE_TOKEN: GenericToken<MyService>;
export declare const ANNOTATED_TOKEN: GenericToken<MyService>;
export declare const ASSERTION_TOKEN: GenericToken<MyService>;
export declare class AnotherService {
value: number;
}
export declare const ANOTHER_TOKEN: GenericToken<AnotherService>;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './consumer.js';

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "test-cross-package-generics",
"version": "0.0.1",
"type": "module",
"dependencies": {
"test-package": "1.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { GenericToken } from 'test-package';

export class MyService {
constructor(private name: string) {}
getName() { return this.name; }
}

// This should preserve the generic type as GenericToken<MyService>
// Before the fix, this would be compiled as 'any' due to module resolution issues
export const SERVICE_TOKEN = new GenericToken<MyService>('MyService');

// These explicit annotations should work regardless
export const ANNOTATED_TOKEN: GenericToken<MyService> = new GenericToken<MyService>('MyService');
export const ASSERTION_TOKEN = new GenericToken<MyService>('MyService') as GenericToken<MyService>;

// Test with different generic types
export class AnotherService {
value = 42;
}

export const ANOTHER_TOKEN = new GenericToken<AnotherService>('AnotherService');
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './consumer.js';

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"target": "ES2022",
"lib": ["ES2022", "DOM"],
"module": "ESNext",
"moduleResolution": "bundler",
"declaration": true,
"strict": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true
},
"include": ["src/**/*"]
}