Skip to content

Duplicate of #47669. Arrow functions do not support overloading yet. #56968

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

Closed
craigphicks opened this issue Jan 6, 2024 Β· 2 comments
Closed

Duplicate of #47669. Arrow functions do not support overloading yet. #56968

craigphicks opened this issue Jan 6, 2024 Β· 2 comments

Comments

@craigphicks
Copy link

craigphicks commented Jan 6, 2024 β€’

πŸ”Ž 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

https://www.typescriptlang.org/play?ts=3.7.5&ssl=24&ssc=27&pln=23&pc=27#code/PTAEBMFMDMEsDtLlAewG6QE4BsUENkAXATwAdIAoE80AWWIHkAZUAXlFAG8KOOAKAB4AuAIwBKUQG4evQUIBME+dN6g5AZgnrpAXwoUAxingBnQqAC2xaHiH1mbNbZEAfeS81sAfFwohVAH5BAX5gACpkkKAA5Hy2oCKgLqDySaCerD6JyanJ6mkArvBQcIjg0aCwJqDwKOZ4JiawAObweABG2FGEKKDUUdH2TNEAdKG8ETTR2Slp+clFJQhIFVU1daANTa0dXX29-TEio+OqkwOLMMvlldW19Y0tbZ3dB5FHo4QmfPLq8ooyDgmADusEIBgAFnExJxQAYGlEREJQJhIIQCph4AlJHCESlkaj0ZiUjj4SYouoCWiMVjtKA9Ko8Dj-IgMJgADSgACq8FReEhuyiRigEDRkAMhCQIy+fAA7AAGeSysQUPRGUzmKzQdp2RgsdhxURuDxiby+fy8YIhfznGKGhJpXLpU2ZB05OaFYpXMqrO4bLZPQX7PrvQZ6k4W0C26aOj0LL2lFa3dYPbbPPY9ENTY5jSMcaOXRM3Nb3TaPHYvYOHaafb6-f4qvOqZuWq1tq3jABy-uw2DhKBFpDwhAh1UJNM2oDQeGwBUgIzl8vU8tNfGMoAABobXO4Mj5OBuVapYNAnGxWOxxCjqcSRCpeCezxf2Ipr0SscpAZVT3Fz+xPOOxLaF+TKgCykBspy7QFOYpYmBCKDAliDSgEUfICi8qr6OqZiWNYBjIv0KCnlqyAGs4xqaJksIsr0WCYCgmCnBwHY2qG9ozE6e5urMeS+imZZpkGmbVrCHESNiTjIoo0k4vamjInSOgRmA+ahpxcz8aWAYVhmbzZipzbRvIWn+uW6avFmAw5j8fwAqoIJgpC0KwmSiJUu+klufib4TsouLkukHkTkpWHjFABjYHgqLIOgWC4AQoDQEUEqwOubJNOuEIobUoCkAxLwWBQyXwKl66kduEh3sVKWEGlWIVbYMmfiVZUNdY4CGgpwGtXV5UddulEwmBYC5fRjFfo54JQngw3eUivm3qSeLyMFxL+d5lKLbSOJ6HoFBAA

πŸ’» 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.

@craigphicks craigphicks changed the title 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) Jan 6, 2024
@MartinJohns
Copy link
Contributor

Duplicate of #47669. Arrow functions do not support overloading yet.

@craigphicks craigphicks changed the title 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. Jan 6, 2024
@craigphicks
Copy link
Author

Closing as duplicate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants