Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Copy link
Copy link
@ddurschlag6river

Description

@ddurschlag6river
Issue body actions

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.
Reactions are currently unavailable

Metadata

Metadata

Assignees

Labels

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

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions

    Morty Proxy This is a proxified and sanitized view of the page, visit original site.