microsoft / microsoft/TypeScript

Issue with union type

Abierto
#36,846 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Needs Investigation
Lenguaje dominante
Go
Estrellas
111k
Forks
14.4k
Merge medio
1 d 19 h
PR fusionados (30 d)
117

Descripción

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

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza con el enlace proporcionado a Playground y la reproducción mínima en TypeScript bajo las opciones del compilador indicadas. Rastrea cómo se comprueban la búsqueda indexada de Service y el tipo condicional genérico, y después añade un caso de regresión para el error de assignability informado. El trabajo estará terminado cuando el ejemplo pase la comprobación de tipos sin el diagnóstico incorrecto de tipo intersección y conserve el tipo de retorno mostrado.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
typescript
Área
compilers
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.