microsoft / microsoft/TypeScript

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

Aperta
#38,359 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Bug Domain: Conditional Types
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

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:

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Riproduci l’esempio fornito di ereditarietà generica nel TypeScript Playground collegato utilizzando le versioni di TypeScript elencate. Poiché non sono indicati file del repository né test, individua il percorso di type-checking del compilatore per i tipi condizionali e le chiamate ai metodi della superclasse, quindi aggiungi un test di regressione che mostri che l’esempio viene compilato senza le diagnostiche segnalate.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.