microsoft / microsoft/TypeScript

[3.9.2] Regression - combination of mapped type, intersection and generic produces error under infer

Abierto
#38,460 2 comentarios 1 reacción 1 asignado Ver en GitHub

@weswigham ya está trabajando en esto.

Desde el 5/6/2020.

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

Descripción

TypeScript Version: 3.9.2, 3.9.1-rc (works in 3.8.3)

Search Terms: infer, intersection, generic, mapped types

Code

type UnknownExtensions = Record<string, unknown>;

type IdentityMappedType<T> = {
  [P in keyof T]: T[P];
};

type Structure<Extensions extends UnknownExtensions> =
  IdentityMappedType<{ someBaseProperty: string } & Extensions>;

type InferStructureExtensions<TStructure extends Structure<UnknownExtensions>> =
  TStructure extends Structure<infer U> ? U : never; // Error: Type 'U' does not satisfy the constraint 'Record<string, unknown>'.

// Without mapped

type Structure_WithoutMappedType<Extensions extends UnknownExtensions> = 
  { someBaseProperty: string } & Extensions;

type InferStructureExtensions_WithoutMappedType<TStructure extends Structure<UnknownExtensions>> =
  TStructure extends Structure_WithoutMappedType<infer U> ? U : never; // Ok

// Without generic intersection

type Structure_WithoutGenericIntersection<Extensions extends UnknownExtensions> = 
  IdentityMappedType<{ someBaseProperty: string }>;

type InferStructureExtensions_WithoutGenericIntersection<TStructure extends Structure<UnknownExtensions>> =
  TStructure extends Structure_WithoutMappedType<infer U> ? U : never; // Ok

// Without concrete intersection

type Structure_WithoutConcreteIntersection<Extensions extends UnknownExtensions> = 
  IdentityMappedType<Extensions>;

type InferStructureExtensions_WithoutConcreteIntersection<TStructure extends Structure<UnknownExtensions>> =
  TStructure extends Structure_WithoutMappedType<infer U> ? U : never; // Ok

// With less strict typing of the generic

type AnyExtensions = Record<string, any>;

type Structure_WithAnyExtensions<Extensions extends AnyExtensions> = 
  IdentityMappedType<Extensions>;

type InferStructureExtensions_WithAnyExtensions<TStructure extends Structure<AnyExtensions>> =
  TStructure extends Structure_WithoutMappedType<infer U> ? U : never; // Ok

Expected behavior:

Should be able to infer U, given the code above.

Actual behavior:

An error is produced: "Error: Type 'U' does not satisfy the constraint 'Record<string, unknown>'."

Note, the example given outlines similar situations where no error is produced.

Playground Link: https://www.typescriptlang.org/play?ts=3.9.0-beta#code/C4TwDgpgBAqgdgazgewO5wKIA9gTgZwEtkCoBeKAJQgGNkAnAEwB59h7C4BzAGigFdEKdAD4A3ACgJoSFACSjPMEKgAsgEMwkRgBVwEZjpHkoAbwlQoAbQAKUTlAQQQyAGZQdAXQBcH258kAX0lpfSgAZXZ+GmB+egNsXAJiUggcPEZ8WCE0THTkknxjMgt5RThlNU1tPUhmUyh8ZABbCAAhdXwIG3pkSHpQXzYObihAqAAyKES8IkLxKRloOThXCHpI+mjY+JmCgkNN7bjoNKTMiKiYk+Z4JFy9uYIRYtKdI+v4qDOMrI+dgycNb0WDGAD8sCgvjgEAAbusxFAAPRI6b0Xr0Xy1aAAchgOKgjGQECyKGAjXUynwrhAUGAAAtoHQCOx1JxyTjqHQmKx2JxeAIcqIcQA6KQoqAAdRU9OQ-HJzWqEEYizC-xOAH1pQy5cANFpldjmI8UlkfnALndhHkkk8iiZSg0mq0Ol0en11oNGnzRuMpibCiElvJVut1bt8natTLdfqavpDlcAd98hdwwYrQ9I6aXuQ3umU+c-knNdrZfK44aE0D1qCoBCYFCoDD4fRERKAPIIcWosu6qBcPDrQg0ewVdZdGIpVWydPRnXygDiQ44NBWuHok+UJGN2cKhd+2Xu6ADzwdlgUShUIErugTTpa7U63V6-S9w35YwWoVkK2B6dPfB53LYBlxhVd1wnWhtzgRMtk+U5U2LeCAVuIUbVmHNXksd4Sy+c00zwiBgNjJU7zqGsQRgcFIWhOEEWRVEux7KUY3lKBmRoeJcDHDct2nH9oDnPt5QAYRILiIFwSDN2glJd1tU0DwtLJMxPPczwoUpLwqa9byNQDv2DP8wyIwCSLEiTuIgGT+J3XCUJOZTCMc+I0OPDD9iKbCPALAjkOOeILL1MijUousGybFsGM7bsJAlMsoAAGxJLIPxiOlwE-Nw6UZAcVxHGdoAAQTgEBAJMLkGBYD9uD4dQyqMtUiOA0ryo0-AFMw-d-KgNrDPPMoryqA1yISDqmt-UMNjMjrWrKwC4MCxCi0uVyDH6iafIc5bnIChDgv06tpoi2jm3ottGKgLsgA

Related Issues:

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.

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.