-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Unable to create a function wrapper #54569
Comments
Okay so the problem here is that You really need #27808 to deal with this properly. Short of that, youβll just have to ts-ignore the error. Footnotes
|
Nevertheless it does typecheck the calls correctly, so everything works exactly as I expect it to, except the error message. |
Yes, but this call also typechecks: run("one" as "one" | "two", 123); β¦which therefore makes the inner call to |
I'd say #47109 is the current way to express this sort of thing: type TestArgs = { [K in keyof Test]: Parameters<Test[K]> }
type TestRet = { [K in keyof Test]: ReturnType<Test[K]> }
const test: { [K in keyof Test]: (...args: TestArgs[K]) => TestRet[K] } = new Test();
export function run<K extends keyof Test>(name: K, ...args: TestArgs[K]): void {
const fn = test[name];
const ret = fn(...args);
// ^? const ret: TestRet[K]
}
Could you elaborate? On the face of it it looks false: TS provides type assertions, the |
I'm afraid I don't understand what is unsafe here. It correctly points out that you can't run one with a number.
I am once again finding myself at the bottom of the confidence curve. Hats off, this seems to check out.
Which one of these can tell TS to ignore this particular error and nothing else, again? |
It does not: See Playground @jcalz Worth noting that your solution suffers from the exact same unsoundness as shown here, despite removing the error in the implementation. This pattern really does beg for |
I see the problem now. Strange how As it's my own code, this solution still seems perfectly safe for my particular use case. |
In the first case, By giving a union of keys for |
That actually makes sense. Thank you. I suppose I should go ahead and close the issue now. |
Bug Report
π Search Terms
2556, tuple, union
π Version & Regression Information
n/a
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
TypeScript shows an error about the spread argument.
I've seen a bunch of similar issues, however I left unsatisfied with them. My specific concerns are these:
run('five')
run('one')
run('one', 123)
run('ten', null, window)
as demonstrated in the last lineI'm left with no options, and I think this deserves an issue.
π Expected behavior
TypeScript doesn't show an error.
The text was updated successfully, but these errors were encountered: