microsoft / microsoft/TypeScript

TypeScript does not infer the type correctly after two layer Mapped Types.

Abierto
#38,001 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.3k
Merge medio
2 d 4 h
PR fusionados (30 d)
132

Descripción

TypeScript Version: 3.9.0-beta

Search Terms:

Lookup Types, Mapped Types

Code

This is Okay:

interface APIs {
  readonly getUserProfile: {
    readonly Parameter: {
      readonly id: string;
    };
    readonly Response: {
      '200': {
        readonly id: string;
        readonly name: string;
        readonly gender: boolean;
      };
    };
  };
}

export type ValueType<T extends object> = T[keyof T];

export type PropertyType<T extends object, K extends string> = K extends keyof T
  ? T[K]
  : never;

type MyFetch = {
  [operationId in keyof APIs]: (
    param: PropertyType<APIs[operationId], 'Parameter'>,
  ) => Promise<
    ValueType<PropertyType<APIs[operationId], 'Response'>>
  >;
};

And this is Error: Type 'APIs[server][string]' is not assignable to type 'object'


interface APIs {
  readonly server1: {
    readonly getUserProfile: {
      readonly Parameter: {
        readonly id: string;
      };
      readonly Response: {
        '200': {
          readonly id: string;
          readonly name: string;
          readonly gender: boolean;
        };
      };
    };
  };
}

export type ValueType<T extends object> = T[keyof T];

export type PropertyType<T extends object, K extends string> = K extends keyof T
  ? T[K]
  : never;

type MyFetch = {
  [server in keyof APIs]: {
    [operationId in keyof APIs[server]]: (
      param: PropertyType<APIs[server][operationId], 'Parameter'>,
    ) => Promise<
      ValueType<PropertyType<APIs[server][operationId], 'Response'>>
    >;
  };
};

And this is Ok

interface APIs {
  readonly server1: {
    readonly getUserProfile: {
      readonly Parameter: {
        readonly id: string;
      };
      readonly Response: {
        '200': {
          readonly id: string;
          readonly name: string;
          readonly gender: boolean;
        };
      };
    };
  };
}

export type ValueType<T extends object> = T[keyof T];

export type PropertyType<T extends object, K extends string> = K extends keyof T
  ? T[K]
  : never;

type MyFetch = {
  [server in keyof APIs]: {
    [operationId in keyof APIs[server]]: (
      param: PropertyType<{} & APIs[server][operationId], 'Parameter'>,
    ) => Promise<
      ValueType<{} & PropertyType<{} & APIs[server][operationId], 'Response'>>
    >;
  };
};

Expected behavior:

remove three {} & and no error.
Actual behavior:

Playground Link:

Ok Playground Link 0

Error Playground Link

Ok Playground Link

Related Issues:

https://github.com/pirix-gh/ts-toolbelt/issues/107

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

Empieza ejecutando el Error Playground Link con TypeScript 3.9.0-beta y compáralo con los dos Ok Playground Links. Investiga la ruta de inferencia de mapped-type y lookup-type responsable del error de constraint reportado. Se considera terminado cuando las tres intersecciones {} se puedan eliminar sin errores en el ejemplo reproducido.

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.