Skip to content

Different behavior between inline type and type alias #29677

Closed
@ozum

Description

@ozum

TypeScript Version: 3.4.0-dev.20190131

I'm trying to find base types of some class properties. For example, for a given X[], trying to find X.

I use same definition inline and as a type alias. Type alias has error whereas inline type works.

Search Terms: Type alias, different behavior, conditional

Code

Below is simplified version of the actual code. Generics A1 and A2 in class method whereRelated are equal, except A1 uses inline type and A2 uses a type alias.

interface RelationFields {
  x: A;
  y: A[];
  z: A[];
}
type Name = keyof RelationFields;
type ShouldA<RF extends RelationFields, N extends Name> = RF[N] extends A[] ? RF[N][0] : never;

class A {
  x: A;
  y: A[];
  z: A[];

  whereRelated<
    RF extends RelationFields = RelationFields,
    N extends Name = Name,
    A1 extends A = RF[N] extends A[] ? RF[N][0] : never, // Works
    A2 extends A = ShouldA<RF, N> // Type is same as A1, but is not assignable to type A
  >(): number {
    return 1;
  }
}

Expected behavior:

Either both A1 and A2 work or both have errors.

Actual behavior:

A1 works, but A2 has an error: ... Type 'RelationFields[N][0]' is not assignable to type 'A'.

Playground Link: See here

Related Issues: #15973 seems similar, but I can not be sure. If this is an expected behavior, an advise how to use a type alias is appreciated.

Thanks,

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDomain: Conditional TypesThe issue relates to conditional typesFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions