Skip to content

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

Closed
@ddurschlag6river

Description

@ddurschlag6river

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.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issueRescheduledThis issue was previously scheduled to an earlier milestone

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions