You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Make it possible to infer an overloaded function when trying to assign to one. This would make it more convenient to reuse overloaded functions.
When the function has explicitly typed inputs and outputs it would be able to reuse the same logic that's used to check if an implementation signature of an overloaded function is compatible with its overload signatures. When an argument or return type needs to be inferred then a union of all possible values is probably what should be inferred. This is already partially implemented.
This proposal is complimentary to other proposals like #59350 or #54539 but it's intentionally more limited in scope to some others. I'm aware of the overlap but I see this as a separate proposal because it's a fix for specifically the inference side of things, rather than anything more broad.
π Motivating Example
Currently this is what happens when you try:
interfaceSomeLongOverload{(x: string): string;(y: number|number[]): number[];(z: string[]): string;(a: number,b?: number): [number,number];}declareclassBaseClass{someLongOverload1: SomeLongOverload;someLongOverload2: SomeLongOverload;}classSubClassextendsBaseClass{someLongOverload1: SomeLongOverload=(a)=>{ ... }// ^ ^ This infers correctly as `string | number | number[] | string[] | number`// ^// ^ Type '(a: string | number | number[] | string[]) => string | number[] | string | [number, number]' is not assignable to type 'SomeLongOverload'.// Type 'string | number[] | [number, number]' is not assignable to type 'string'.// Type 'number[]' is not assignable to type 'string'.someLongOverload2: SomeLongOverload=(a,b)=>{ ... }// ^ ^ ^ infers as `number | undefined`// ^ ^// ^ ^ infers as `number`// ^// ^ Type '(a: number, b: number | undefined) => string | number[] | string | [number, number]' is not assignable to type 'SomeLongOverload'.// Target signature provides too few arguments. Expected 2 or more, but got 1.}
Ideally neither of these examples would error and the second example would infer correctly. It seems that the current way inference works is to extract the types from all overloads with the same arity or greater. Given the nature of function overloads this should probably be changed.
If there are concerns with automatically inferring overloads in the general case, e.g. for something like takesLongOverloadedFunction((a, b) => { ... }) then it could be limited to cases of the form const x: SomeLongOverload = (...) => ...;. I can understand arguments both directions for takesLongOverloadedFunction because it could easily mask obvious errors.
π» Use Cases
What do you want to use this for? Being more DRY
What shortcomings exist with current approaches? Duplication of code. Increased difficulty to refactor.
What workarounds are you using in the meantime? Being much more verbose.
The text was updated successfully, but these errors were encountered:
LukeAbby
changed the title
Enable Inferring Function Overloads
Enable Inferring from Overloaded Functions
Dec 21, 2024
π Search Terms
inferring, function overload
β Viability Checklist
β Suggestion
Make it possible to infer an overloaded function when trying to assign to one. This would make it more convenient to reuse overloaded functions.
When the function has explicitly typed inputs and outputs it would be able to reuse the same logic that's used to check if an implementation signature of an overloaded function is compatible with its overload signatures. When an argument or return type needs to be inferred then a union of all possible values is probably what should be inferred. This is already partially implemented.
This proposal is complimentary to other proposals like #59350 or #54539 but it's intentionally more limited in scope to some others. I'm aware of the overlap but I see this as a separate proposal because it's a fix for specifically the inference side of things, rather than anything more broad.
π Motivating Example
Currently this is what happens when you try:
Playground
Ideally neither of these examples would error and the second example would infer correctly. It seems that the current way inference works is to extract the types from all overloads with the same arity or greater. Given the nature of function overloads this should probably be changed.
If there are concerns with automatically inferring overloads in the general case, e.g. for something like
takesLongOverloadedFunction((a, b) => { ... })
then it could be limited to cases of the formconst x: SomeLongOverload = (...) => ...;
. I can understand arguments both directions fortakesLongOverloadedFunction
because it could easily mask obvious errors.π» Use Cases
The text was updated successfully, but these errors were encountered: