Skip to content

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

Closed
@craigphicks

Description

@craigphicks

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions