microsoft / microsoft/TypeScript

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

Open
#38,460 2 comments 1 reaction 1 assignee View on GitHub

@weswigham is already working on this.

Since Jun 5, 2020.

Needs Investigation Rescheduled
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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:

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.