Preview:ts class X { foo(xyz?: false): number foo(xyz: true): string { return xyz === true ? "abc" : 10 } }
You can choose specific lines to embed by selecting them before copying the link.
6 messages · Page 1 of 1 (latest)
Preview:ts class X { foo(xyz?: false): number foo(xyz: true): string { return xyz === true ? "abc" : 10 } }
Is it possible to make this work?
The implementation needs to handle the most generic case. Something like this:
class X {
foo(xyz?: false): number
foo(xyz: true): string
foo(xyz?: boolean): string | number {
return xyz === true ? 'abc' : 10
}
}
Oh cool, thanks Evan. It looks like the compiler won't check for correctness against overloads though, is that right? e.g. if I change the 10 to "10" it won't check the false branch is correctly returning a number
yes, overloads aren't checked
thanks 🙂