microsoft / microsoft/TypeScript

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

Đang mở
#38,460 2 bình luận 1 reaction 1 người được giao Xem trên GitHub

@weswigham đang làm issue này rồi.

Từ ngày 5/6/2020.

Needs Investigation Rescheduled
Ngôn ngữ chính
Go
Star
111k
Fork
14.3k
Merge trung bình
2 ngày 4 giờ
Pull request đã merge (30 ngày)
132

Mô tả

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:

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.