Skip to content

[Bug] Conditional types throw error since TS 4.9.xΒ #51614

Description

@jxn-30

Bug Report

πŸ”Ž Search Terms

conditional types, 2339

πŸ•— Version & Regression Information

  • This changed between versions 4.8.4 and 4.93
  • This bug also occurs on nightly version 5.0.0-dev.20221121

⏯ Playground Link

Playground link with relevant code and test cases

πŸ’» Code

// Errors in TS 4.9.3 but not in 4.8.4
interface A {}
type B = A extends Record<'foo', string> ? A['foo'] : string; // Error: Property 'foo' does not exist on type 'A'.(2339)

// Succeeds in TS 4.9.3 and 4.8.4
interface C { foo: number; }
type D = C extends Record<'foo', string> ? C['foo'] : string; // Correct: string

// Succeeds in TS 4.9.3 and 4.8.4
interface E { foo: string; }
type F = E extends Record<'foo', string> ? E['foo'] : string; // Correct: string

// Succeeds in TS 4.9.3 and 4.8.4
interface G { foo: 'bar'; }
type H = G extends Record<'foo', string> ? G['foo'] : string; // Correct: 'bar'

πŸ™ Actual behavior

interface A {}
type B = A extends Record<'foo', string> ? A['foo'] : string;

errors with 2339: Property 'foo' does not exist on type 'A'.

This change causes @vuejs/pinia to fail as one can see in vuejs/pinia#1814

πŸ™‚ Expected behavior

interface A {}
type B = A extends Record<'foo', string> ? A['foo'] : string;

returns with B being an alias for type string.

Metadata

Metadata

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions