-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Compiler fails to infer type in overloaded functions #33704
Comments
Duplicate #14515 |
Overloads are a real pain in the butt, honestly. function foo(...args : []|[number, number]): void {
if (args.length == 0) return;
const [a, b] = args;
console.log(a.length + b.length);
}
foo(); //OK
foo(1,2); //OK
foo(1); //Error I'm on mobile now, so I might have made a mistake but the above should work |
Another example I posted on Gitter earlier on, interface Data {
someUsefulPieceOfInfo : string
}
function callback (...args : [Error]|[null|undefined, Data]) {
if (args[0] == undefined) {
console.log(args[1].someUsefulPieceOfInfo);
} else {
console.error(args[0].message);
}
}
callback(new Error("blah"));
callback(undefined, { someUsefulPieceOfInfo : "Hello, world" }); |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
@AnyhowStep in response to #33704 (comment) Is there any workaround for when you are using generics?
I'd like be able to define my overloads like above and be able to call |
Seems like the same pattern as See, #33707 |
TypeScript Version: 3.6.3
Search Terms: overload overloaded infer type flow
Code
Expected behavior:
The code compiles (the compiler deduces
b != null
from the set of possible overloads)Actual behavior:
bug.ts:5:28 - error TS2532: Object is possibly 'undefined'.
Playground Link:
https://www.typescriptlang.org/play/#code/GYVwdgxgLglg9mABMOcAUBKAXIgbnGAEwChRJYFlU0BDHAZygCcYwBzAGkQCMHnW22PARJlo8JCnQ0A-Hxbsu3OYkYLBOfEUQBvYogOIYwRGgCENDIiYBTKCCZh9hiAnpwANjYB0HuG1pfG3YoAAtEAGoeIJDQjGIAXyA
Related Issues: N/A
The text was updated successfully, but these errors were encountered: