Closed
Description
π Search Terms
is: issue: in-title: defined overload type
is:issue label:bug in-title: overload type
π Version & Regression Information
Same in versions 3.75 ~ 5.3.3
Slight differences in errors before that.
β― Playground Link
π» Code
// defined overload type
type MyOL = {
(x:1):1;
(x:2):2;
(x:3):3;
}
const myfa:MyOL = (a:1|2|3) => {
// ~~~~
// Type '(a: 1 | 2 | 3) => 1 | 2 | 3 | undefined' is not assignable to type 'MyOL'.
// Type '1 | 2 | 3 | undefined' is not assignable to type '1'.
// Type 'undefined' is not assignable to type '1'.ts(2322)
switch(a){ case 1: return 1; case 2: return 2; case 3: return 3; }
a; // never, Unreachable code detected.ts(7027)
}
const myfb:MyOL = (a:1|2|3) => {
// ~~~~
// Type '(a: 1 | 2 | 3) => 1 | 2 | 3 | undefined' is not assignable to type 'MyOL'.
// Type '1 | 2 | 3 | undefined' is not assignable to type '1'.
// Type 'undefined' is not assignable to type '1'.ts(2322)
// ~~~~~~~~~~~~~~
// Not all code paths return a value.(7030) (on `(a:1|2|3) => {`)
if (a === 1) return 1;
if (a === 2) return 2;
if (a === 3) return 3;
a; // never, but not shown as unreachable
}
const myfc: typeof myfd = (a:1|2|3)=>{ // no error
// ~~~~
// Type '(a: 1 | 2 | 3) => 1 | 2 | 3' is not assignable to type '{ (a: 1): 1; (a: 2): 2; (a: 3): 3; }'.
// Type '1 | 2 | 3' is not assignable to type '1'.
// Type '2' is not assignable to type '1'.(2322)
switch(a){ case 1: return 1; case 2: return 2; case 3: return 3; }
}
// declared overload function version has no problem
function myfd(a:1):1;
function myfd(a:2):2;
function myfd(a:3):3;
function myfd(a:1|2|3){ // no error
switch(a){ case 1: return 1; case 2: return 2; case 3: return 3; }
}
π Actual behavior
Errors on the implementations of myfa
and myfb
and myfc
, which are typed by a defined function overload type.
However, no errors on the declared function overload myfd
.
π Expected behavior
No errors at all.
Additional information about the issue
Workaround:
const myWorkaround = ((a:1|2|3) => {
switch(a){ case 1: return 1; case 2: return 2; case 3: return 3; }
}) as any as MyOL;
Advice welcome on a better title.
Activity
[-]Inability to exhaustively list cases of parameter in function implementation when type is a defined overload type (as opposed to declared overload type[/-][+]Errors on the implementation for a defined overload type (c.f., no errors on declared implementation)[/+]MartinJohns commentedon Jan 6, 2024
Duplicate of #47669. Arrow functions do not support overloading yet.
[-]Errors on the implementation for a defined overload type (c.f., no errors on declared implementation)[/-][+]Duplicate of #47669. Arrow functions do not support overloading yet.[/+]craigphicks commentedon Jan 6, 2024
Closing as duplicate.