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

Strange behavior of function overloading and union type #50488

Closed
Jack-Works opened this issue Aug 27, 2022 · 2 comments
Closed

Strange behavior of function overloading and union type #50488

Jack-Works opened this issue Aug 27, 2022 · 2 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@Jack-Works
Copy link
Contributor

Bug Report

Strange behavior of function overloading and union type

πŸ”Ž Search Terms

function overloading union type

πŸ•— Version & Regression Information

  • This changed between versions 3.5 (good) and 3.6 (bad)

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

import { Canvas, CanvasRenderingContext2D as NodeCanvasRenderingContext2D } from 'canvas'

let ctx: CanvasRenderingContext2D | NodeCanvasRenderingContext2D | null = null

function test(canvas: HTMLCanvasElement | Canvas) {
    ctx = canvas.getContext('2d')
    // error??
}
function test2(canvas: HTMLCanvasElement | Canvas) {
    // works!!
    if (canvas instanceof HTMLCanvasElement) {
        ctx = canvas.getContext('2d')
    } else {
        ctx = canvas.getContext('2d')
    }
}

πŸ™ Actual behavior

Type 'NodeCanvasRenderingContext2D | RenderingContext | null' is not assignable to type 'CanvasRenderingContext2D | NodeCanvasRenderingContext2D | null'.

πŸ™‚ Expected behavior

No error

@RyanCavanaugh RyanCavanaugh added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Aug 29, 2022
@RyanCavanaugh
Copy link
Member

Function type merging produces a single signature in this case, and that single signature doesn't keep around enough information.

The only way to process this "correctly" would be combinatorially explosive, so that doesn't happen.

@Jack-Works
Copy link
Contributor Author

but why does it work on TS 3.5? πŸ€”

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

2 participants