GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Method overloads fail to resolve correctly when using mapped types with conditional generics and optional parameters · Issue #62377 · microsoft/TypeScript Open Labels Needs More InfoThe issue still hasn't been fully clarifiedThe issue still hasn't been fully clarified Description🔎 Search Termsmethod overload, generic mapped types, conditional generics, overload resolution, optional parameters, Parameters utility type, ReturnType, ConfigurableAPI, overload lost mapped type, call method generic, generic constraint overload 🕗 Version & Regression Information
⏯ Playground Link💻 Code// Minimal reproduction showing the bug class API<T extends Record<string, (...args: any[]) => any>> { constructor(private methods: T) {} // Method overloads for better DX call<K extends keyof T>(method: K): ReturnType<T[K]>; call<K extends keyof T>(method: K, ...args: Parameters<T[K]>): ReturnType<T[K]>; call<K extends keyof T>(method: K, ...args: any[]): ReturnType<T[K]> { return this.methods[method](...args); } } // Test with various method signatures const api = new API({ // No parameters getString: () => "hello", // Required parameter getNumber: (multiplier: number) => 42 * multiplier, // Optional parameter processData: (data: string, options?: { uppercase?: boolean }) => options?.uppercase ? data.toUpperCase() : data, // Multiple parameters with optional complexMethod: (a: number, b: string, c?: boolean) => c ? `${b}-${a}` : `${a}-${b}` }); // ❌ BUG: Overload resolution fails // Case 1: No parameters - should match first overload const str = api.call('getString'); // Error: Expected 2 arguments, but got 1. // Case 2: Optional parameter - fails const data1 = api.call('processData', 'test'); // Error: Argument of type 'string' is not assignable to parameter of type 'never'. // Case 3: Multiple parameters with optional const result1 = api.call('complexMethod', 1, 'test'); // Error: Argument of type 'number' is not assignable to parameter of type 'never'. 🙁 Actual behaviorTypeScript fails to correctly resolve method overloads when using generic mapped types with Parameters<T[K]> utility type:
The generic constraint with 🙂 Expected behaviorThe overload resolution should work correctly based on the number and types of arguments:
The compiler should be able to:
This pattern is essential for type-safe API wrappers, RPC clients, and plugin systems. Additional information about the issueThis issue significantly impacts developer experience when building type-safe wrappers around dynamic method collections. Real-world use cases affected:
Current workarounds (all suboptimal):
Related issues:
Without proper overload resolution, library authors must sacrifice either type safety or developer experience. Activity
MetadataMetadataAssigneesLabelsNeeds More InfoThe issue still hasn't been fully clarifiedThe issue still hasn't been fully clarified TypeProjectsMilestoneRelationshipsDevelopmentIssue actions |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |