Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
nth-commit opened this issue May 11, 2020 · 2 comments
Assignees
Labels
Needs Investigation This issue needs a team member to investigate its status. Rescheduled This issue was previously scheduled to an earlier milestone

Comments

@nth-commit
Copy link

nth-commit commented May 11, 2020

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:

@nth-commit nth-commit changed the title [3.9.1-rc] Regression - combination of mapped type, intersection and generic produces error under infer [3.9.2] Regression - combination of mapped type, intersection and generic produces error under infer May 13, 2020
@fox1t
Copy link

fox1t commented May 13, 2020

I am facing this issue on 3.9.2 using https://github.com/sinclairzx81/typebox

Type 'unknown' is not assignable to type 'TSchema'.
    Type 'unknown' is not assignable to type 'TNull'.
      Type 'unknown' is not assignable to type '{ type: "null"; }'.
        Type 'U' is not assignable to type 'TNull'.
          Type 'unknown' is not assignable to type 'TNull'.
            Type 'unknown' is not assignable to type '{ type: "null"; }'.
              Type 'U' is not assignable to type '{ type: "null"; }'.
                Type 'unknown' is not assignable to type '{ type: "null"; }'.

Works fine in 3.8.3

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Jun 5, 2020
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.0 milestone Jun 5, 2020
@RyanCavanaugh RyanCavanaugh added the Rescheduled This issue was previously scheduled to an earlier milestone label Aug 31, 2020
@RyanCavanaugh RyanCavanaugh removed this from the TypeScript 4.8.0 milestone Feb 1, 2023
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 5.1.0 milestone Feb 1, 2023
@Andarist
Copy link
Contributor

Andarist commented Mar 6, 2023

This getReducedType call is responsible for the circularity.

  1. we are in the getResolvedBaseConstraint for U and that creates a recursion stack
  2. that leads to getInferredTypeParameterConstraint for U
  3. and eventually circles back to getResolvedBaseConstraint through that mentioned getReducedType call

Not a lot of cases break without that getReducedType call (mainly types like Partial<never> are not reduced to just never) so perhaps it could be replaced with a different strategy. 🤔 Or maybe under certain conditions we could detect the cycle and skip re-requesting that constraint?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Investigation This issue needs a team member to investigate its status. Rescheduled This issue was previously scheduled to an earlier milestone
Projects
None yet
Development

No branches or pull requests

6 participants