-
Notifications
You must be signed in to change notification settings - Fork 141
Description
Hello! First just want to say thanks for the great functionality. Ran into a types issue with object literals containing functions after using the package to install one of my JSR packages (@tidy-ts/dataframe). Had Claude help me provide a little write up:
Issue Summary: TypeScript Function Overload Type Loss in JSR npm Distribution
Problem Description
When a JSR package exports object literals containing imported functions, the TypeScript compilation process for npm distribution loses function signature information, resulting in all function properties being typed as any
instead of preserving their original overloaded signatures.
Reproduction Steps
-
Source TypeScript (works correctly in Deno):
// stats.ts import { sum } from "./sum.ts"; // sum has multiple overloads export const stats = { sum, // Should preserve typeof sum with all overloads };
-
Expected behavior (Deno direct import):
const stats: { sum: { (value: number): number; (values: CleanNumberArray): number; (values: NumbersWithNullable, remove_na: true): number; // ... more overloads }; }
-
Actual behavior (JSR npm distribution):
const stats: { readonly sum: any; // Type information lost }
Root Cause Analysis
The issue occurs in JSR's TypeScript-to-npm compilation pipeline:
- Source compilation: JSR compiles TypeScript to JavaScript +
.d.ts
files - Type inference limitation: When TypeScript generates
.d.ts
files for object literals with imported functions, it creates:import { sum } from "./sum.js"; // Value import, not type import export declare const stats: { readonly sum; // No explicit type - becomes `any` };
- Missing type preservation: The generated
.d.ts
doesn't preserve thetypeof sum
relationship that would maintain function signatures
Technical Details
- Works in Deno: Direct TypeScript import preserves all type information
- Fails in npm: Compiled
.d.ts
files lose function signature details - Affected pattern: Object literals with imported function values
- Scope: Any exported object containing functions with overloads
Workaround Solution
Add explicit type annotations using typeof
to preserve function signatures:
export const stats: {
readonly sum: typeof sum; // Preserves all overloads
readonly mean: typeof mean;
// ...
} = {
sum,
mean,
// ...
};
Suggested JSR Improvements
- Enhanced type preservation: Improve the TypeScript compilation process to better preserve
typeof
relationships in generated.d.ts
files - Type import handling: Consider using type-only imports in generated
.d.ts
files where appropriate - Documentation: Add guidance for package authors about this limitation and the workaround
I imagine this might affect any JSR package that exports objects containing functions with overloads. Haven't checked that out myself though. Thanks again for the great work.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status