Skip to content
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

function overloads implemented with interface will throw error #55358

Closed
Singloo opened this issue Aug 14, 2023 · 1 comment
Closed

function overloads implemented with interface will throw error #55358

Singloo opened this issue Aug 14, 2023 · 1 comment

Comments

@Singloo
Copy link

Singloo commented Aug 14, 2023

πŸ”Ž Search Terms

"function overloads", "overloads with interface"

πŸ•— Version & Regression Information

overloads implemented with interface behaves differently

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.3.0-dev.20230814#code/PTAEHUFMBsGMHsC2lQBd5oBYoCoE8AHSAZVgCcBLA1UABWgEM8BzM+AVwDsATAGiwoBnUENANQAd0gAjQRVSQAUCEmYKsTKGYUAbpGF4OY0BoadYKdJMoL+gzAzIoz3UNEiPOofEVKVqAHSKymAAmkYI7NCuqGqcANag8ABmIjQUXrFOKBJMggBcISGgoAC0oACCbvCwDKgU8JkY7p7ehCTkVDQS2E6gnPCxGcwmZqDSTgzxxWWVoASMFmgYkAAeRJTInN3ymj4d-jSCeNsMq-wuoPaOltigAKoASgAywhK7SbGQZIIz5VWCFzSeCrZagNYbChbHaxUDcCjJZLfSDbExIAgUdxkUBIursJzCFJtXydajBBCcQQ0MwAUVWDEQC0gADVHBQGNJ3KAALygABEAAkYNAMOB4GRonzFBTBPB3AERcwABS0+mM9ysygc9wASmCGQUZGSDCWADEuLBQABvRQlFVkZj5UAARh1Tr5zr5AG5baB7Y7QAAmN38wPexQAX2CEPFNBlNGSFqd5vMPL9jmYOp5AD5rb6EemHTzubzXaAnKh8V4PXyxMJ4z6SgX-cXecHy5BK2Rq2G62jKahG1g2BJ+pBRzSyGwyEq+ZxIHoyHydT6Iz7FInzPVGjiLYH-U7g+6wz7N7Bt14z-uM07Xe7PaeLRfd+Zrw7b6AAD5BrM2pupFsS1LLMKyrflPT7Bt8wAjNWx-Dsux7WsGHrRoqSHLJ4FHecJyncUlQAcnnRcCL1KNFBjMgaCtF9YEDUAoxUHBmg8btQEQcVnGBdgaC+NwzGYdgGGYSB+FgaB1ESDl4D0ERqzpBkmUEWtxX5cAHFQAjhAAOXHPkghUAB5L4yHeQRRK0TsrlQRwFFcaQ8A7DidGGLB9BQBBEGhYRLj4iRxWiEQDA4bEFiYVgOB4IISiAA

πŸ’» Code

interface Func {
  (arg: 1): "1";
  (arg: 2): "2";
}

export const func: Func = (arg) => {
  if (arg === 1) return "1" as const;
  if (arg === 2) return "2" as const;
  throw new Error("never");
};

function func2(arg: 2): "2";
function func2(arg: 1): "1";
function func2(arg: 1 | 2) {
  if (arg === 1) return "1" as const;
  if (arg === 2) return "2" as const;
  throw new Error('never')
}

export { func2 }

πŸ™ Actual behavior

overload implemented with interface will cause error

Type '(arg: 1 | 2) => "1" | "2"' is not assignable to type 'Func'.
  Type '"1" | "2"' is not assignable to type '"1"'.
    Type '"2"' is not assignable to type '"1"'.ts(2322)

πŸ™‚ Expected behavior

these two implementations both should work

@Singloo Singloo changed the title function overloads implemented with interface will throw error. but works with normal way function overloads implemented with interface will throw error Aug 14, 2023
@MartinJohns
Copy link
Contributor

MartinJohns commented Aug 14, 2023

Duplicate of #47669. Overloading is currently not supported for arrow functions.

@Singloo Singloo closed this as completed Aug 14, 2023
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