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

Incorrect intersection narrowing of nongeneric type in conditional type #43427

Closed
RyanCavanaugh opened this issue Mar 29, 2021 · 2 comments Β· Fixed by #43439
Closed

Incorrect intersection narrowing of nongeneric type in conditional type #43427

RyanCavanaugh opened this issue Mar 29, 2021 · 2 comments Β· Fixed by #43439
Assignees
Labels
Bug A bug in TypeScript

Comments

@RyanCavanaugh
Copy link
Member

Bug Report

πŸ”Ž Search Terms

conditional type narrow nongeneric

πŸ•— Version & Regression Information

  • This changed between versions 3.8 and 3.9

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type Q<T> = number extends T ? (n: number) => void : never;
function fn<T>(arg: Q<T>) {
  // Expected: OK
  // Actual: Cannot convert 10 to number & T
  arg(10);
}
// Legal invocations are not problematic
fn<string | number>(m => m.toFixed());
fn<number>(m => m.toFixed());

πŸ™ Actual behavior

Error issued at arg(10), even though by inspection this is always a legal call.

πŸ™‚ Expected behavior

Honestly, not sure.

@RyanCavanaugh
Copy link
Member Author

cc @ahejlsberg / @weswigham

@weswigham
Copy link
Member

number extends T creates a substitution within the true branch - all references to number become number & T, since you just stated that all numbers were Ts. This then affects the apparent type which we get the signature from - the signature looks like (n: number & T) => void. I believe this is an artifact of us not tracking the variance of the position in question when looking for the constrained type at a position, and thus still doing a (covariant) constraint substitution in contravariant positions.

#43439 has a small possible fix.

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

Successfully merging a pull request may close this issue.

2 participants