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

Inference of constrained generic causes open generic to "escape" #31402

Closed
ddurschlag6river opened this issue May 14, 2019 · 1 comment · Fixed by #31455
Closed

Inference of constrained generic causes open generic to "escape" #31402

ddurschlag6river opened this issue May 14, 2019 · 1 comment · Fixed by #31455
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue Rescheduled This issue was previously scheduled to an earlier milestone

Comments

@ddurschlag6river
Copy link

ddurschlag6river commented May 14, 2019

TypeScript Version: 3.5.0-dev.20190514

Search Terms:
constrained generic inference infer keyword
infer open generic
infer constrained generic

Code

interface Synthetic<A, B extends A> {}
type SyntheticDestination<T, U> = U extends Synthetic<T, infer V> ? V : never;
type TestSynthetic = // Resolved to T
    SyntheticDestination<number, Synthetic<number, number>>;

const w: TestSynthetic = undefined // ✓
const x: TestSynthetic = null; // ✓

const y: TestSynthetic = 3; // Type '3' is not assignable to type 'T'.
const z: TestSynthetic = '3'; // Type '"3""' is not assignable to type 'T'.

Expected behavior:

  • TestSynthetic resolved as number
  • w, x, and z invalid with strict
  • y valid

Actual behavior:

  • TestSynthetic resolved as T -- an open generic from the underlying conditional type
  • w and x valid, even with strict
  • y and z error with commented errors

Playground Link:
https://bit.ly/30lTSqk

Related Issues:
#31326

Notes:

  • Removing the extends A constraint causes expected behavior, so that's likely involved somehow.
  • Turning on all strict options in typescript playground causes all four example cases to be errors -- unclear why this is stricter than strict: true in my local tsconfig with @next.
  • I'm not 100% sure if a conditional type that resolves to an open generic is allowed -- I was certainly surprised to see it. If it is, this still seems to be a bug in conditional type resolution.
@jack-williams
Copy link
Collaborator

Related #31337. I think this issue still exists with that fix

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label May 15, 2019
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 3.6.0 milestone May 15, 2019
@weswigham weswigham added Bug A bug in TypeScript Fix Available A PR has been opened for this issue and removed Needs Investigation This issue needs a team member to investigate its status. labels Jul 15, 2019
@RyanCavanaugh RyanCavanaugh added the Rescheduled This issue was previously scheduled to an earlier milestone label May 20, 2020
@RyanCavanaugh RyanCavanaugh removed this from the TypeScript 4.0.1 milestone Aug 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue Rescheduled This issue was previously scheduled to an earlier milestone
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants