microsoft / microsoft/TypeScript

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

Open
#38,359 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: Conditional Types
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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:

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the supplied generic inheritance example in the linked TypeScript Playground using the listed TypeScript versions. Since no repository file or test is named, locate the compiler's type-checking path for conditional types and superclass method calls, then add a regression test showing the example compiles without the reported diagnostics.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.