microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
101.03k stars 12.49k forks source link

TS2345 when calling a superclass' method with a conditional type parameter from a parameterized subclass #38359

Open hstahl opened 4 years ago

hstahl commented 4 years ago

TypeScript Version: 3.8.3 & 4.0.0-dev.20200506

Search Terms: TS2345 generic constructor super superclass method type parameter inheritance conditional type constraint

Code

type FilterKeysOf<T, Condition> = { [K in keyof T]: T[K] extends Condition ? K : never }[keyof T];

type ArrayKeysOf<T> = FilterKeysOf<T, readonly unknown[]>;

interface I {
  foo: string;
  bar: string[];
  baz: number;
}

interface J extends I {
  qux: boolean[];
}

class A<T extends I> {
  protected treasure: T

  constructor(treasure: T) {
    this.treasure = treasure;
  }

  getArrayTreasure<K extends ArrayKeysOf<T>>(fieldName: K): T[K] {
    return this.treasure[fieldName];
  }
}

class B<T extends J> extends A<T> {
  getQuxArrayTreasure(): boolean[] {
    return super.getArrayTreasure('qux');
  }
}

class C extends B<J> {
  anyQux(): boolean {
    return super.getArrayTreasure('qux').some(Boolean);
  }
}

Expected behavior: Compiles without errors.

Actual behavior: TS v3.8.3:

foo.ts:25:35 - error TS2345: Argument of type '"qux"' is not assignable to parameter of type 'T[keyof T] extends readonly unknown[] ? keyof T : never'.

25     return super.getArrayTreasure('qux');
                                     ~~~~~

Found 1 error.

TS v4.0.0-dev.20200506:

foo.ts:25:5 - error TS2322: Type 'T[{ [K in keyof T]: T[K] extends readonly unknown[] ? K : never; }[keyof T]]' is not assignable to type 'boolean[]'.
  Type 'T[T[keyof T] extends readonly unknown[] ? keyof T : never]' is not assignable to type 'boolean[]'.
    Type 'T[keyof T]' is not assignable to type 'boolean[]'.
      Type 'T[string] | T[number] | T[symbol]' is not assignable to type 'boolean[]'.
        Type 'T[string]' is not assignable to type 'boolean[]'.

25     return super.getArrayTreasure('qux');

foo.ts:25:35 - error TS2345: Argument of type '"qux"' is not assignable to parameter of type 'T[keyof T] extends readonly unknown[] ? keyof T : never'.

25     return super.getArrayTreasure('qux');
                                     ~~~~~

Found 2 errors.

Playground Link: Link Related Issues:

RyanCavanaugh commented 4 years ago

@hstahl do you agree this is an equivalent case?

type FilterKeysOf<T, Condition> = { [K in keyof T]: T[K] extends Condition ? K : never }[keyof T];

type ArrayKeysOf<T> = FilterKeysOf<T, readonly unknown[]>;

interface I {
  foo: string;
  bar: string[];
  baz: number;
}

class A<T extends I> {
  protected treasure: T

  constructor(treasure: T) {
    this.treasure = treasure;
  }

  getArrayTreasure<K extends ArrayKeysOf<T>>(fieldName: K): T[K] {
    return this.treasure[fieldName];
  }

  getQuxArrayTreasure() {
    this.getArrayTreasure('bar');
  }
}
hstahl commented 4 years ago

@RyanCavanaugh Yes, your case seems to behave in the same way.