microsoft / microsoft/TypeScript

Issue with union type

Aperta
#36,846 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Needs Investigation
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

TypeScript Version: 3.7.5

Expected behavior:
Type of parameter qm in method Service.getItems should be union type assignable QueryModel<KeyToEntityMap<E>>

Actual behavior:

Argument of type 'QueryModel<KeyToEntityMap<E>>' is not assignable to parameter of type 'QueryModel<{ x: string; }> & QueryModel<{ y: string; }>'.
  Type 'QueryModel<KeyToEntityMap<E>>' is not assignable to type 'QueryModel<{ x: string; }>'.
    Type '{ x: string; }' is not assignable to type 'KeyToEntityMap<E>'.

Code

type Key = "a" | "b";
type KeyToEntityMap<E extends Key> =
    E extends "a" ? { x: string; } :
    E extends "b" ? { y: string; } :
    never;

class QueryModel<T>
{
    filters: [keyof T, string][] = [];
}

class Service<E extends Key>
{
    getItems(qm: QueryModel<KeyToEntityMap<E>>): KeyToEntityMap<E>[]
    {
        return [];
    }
}

type IServiceFactory = { [E in Key]: Service<E> };
class ServicesFactory implements IServiceFactory
{
    a: Service<"a"> = new Service<"a">();
    b: Service<"b"> = new Service<"b">();
}
const sf: IServiceFactory = new ServicesFactory();

class Same<E extends Key>
{
    fail(e: E)
    {
        let queryModel = new QueryModel<KeyToEntityMap<E>>();
        //Here getItems parameter gets wrong intersection type QueryModel<{ x: string; }> & QueryModel<{ y: string; }>
        let items: KeyToEntityMap<E> = sf[e].getItems(queryModel); 
    }
}

Output
"use strict";
class QueryModel {
    constructor() {
        this.filters = [];
    }
}
class Service {
    getItems(qm) {
        return [];
    }
}
class ServicesFactory {
    constructor() {
        this.a = new Service();
        this.b = new Service();
    }
}
const sf = new ServicesFactory();
class Same {
    fail(e) {
        let items = sf[e].getItems(new QueryModel());
    }
}

Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "useDefineForClassFields": false,
    "alwaysStrict": true,
    "allowUnreachableCode": false,
    "allowUnusedLabels": false,
    "downlevelIteration": false,
    "noEmitHelpers": false,
    "noLib": false,
    "noStrictGenericChecks": false,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "esModuleInterop": true,
    "preserveConstEnums": false,
    "removeComments": false,
    "skipLibCheck": false,
    "checkJs": false,
    "allowJs": false,
    "declaration": true,
    "experimentalDecorators": false,
    "emitDecoratorMetadata": false,
    "target": "ES2017",
    "module": "ESNext"
  }
}

Playground Link: Provided

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

Inizia dal link Playground fornito e dalla riproduzione minima in TypeScript con le opzioni del compilatore indicate. Traccia come vengono verificati la ricerca indicizzata di Service e il tipo condizionale generico, quindi aggiungi un caso di regressione per l’errore di assignability segnalato. Il lavoro è completato quando l’esempio supera il controllo dei tipi senza la diagnosi errata di tipo intersezione, preservando il tipo restituito mostrato.

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.