-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make union function types callable. #57400
Comments
Sure it does. It's perfectly legal to call your function this way: D<"execute" | "run">((a: number) => ({ a: 0, b: "string" }), ["abc"]) |
Neither here nor there, but the function signature is also wrong. A closer fit would be function D<T extends "execute" | "run">(arg: Test[T], value: Parameters<Test[T]>[0]) |
Linking to #57372 (comment) as it's relevant here too |
Thank you for the quick answer!
What I would like to know is whether there is a way to create functionWrapper and call inside of it one of the functions which all have overlapping names and one argument only. If we pass to the generic function "functionWrapper" M (the Model name) and F(the function inside the model) as far as I can see type is narrowed enough to preserve all type-safety. It might be the case that the library which generated those declared types isn't perfect (I am not arguing that), because it does not use any inheritance, which might suit the case better, however this is the reality for now. |
Not the way you've written it; the call declare const m: Model1Functions;
functionWrapper<"model1" | "model2", "find" | "create">(m.create, "boolean"); is legal but unsound, thus the implementation must error. Further guidance sort of hinges on why you're not just writing function functionWrapper<Arg>(fn: (arg: Arg) => unknown, value: Arg) {
fn(value) // OK
} Likely your use case boils down to #27808, though |
I get the sense that #30581 might be relevant |
This issue has been marked as "Working as Intended" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
π Search Terms
Each member of the union type has signatures, but none of those signatures are compatible with each other, expression not callable, incompatible signatures,
β Viability Checklist
β Suggestion
Currently typescript does not allow executing generic functions with incompatible union type. However such behavior is not contradicting any type-safety mechanisms. As you will see from the example below there is no way to call such function without "type assertion" and there is no need for asserting the type on this phase of the programming. It would unwrap it's type based on the higher level code that will use this function and would specify the concrete needs for the exact argument types.
π Motivating Example
This shall be allowed in my opinion as it does not put at risk of type mismatch
π» Use Cases
It would allow developers to abstract away function calls and build on top of other libraries that have not taken into account the need for abstracting away common logic and fields
There is no workaround currently. It is possible to narrow the type,however such a solution is not the prefered way and it polutes the codebase.
The text was updated successfully, but these errors were encountered: