Skip to content

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

@nth-commit

Description

@nth-commit

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/C4T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LsgA

Related Issues:

Metadata

Metadata

Assignees

Labels

Needs InvestigationThis issue needs a team member to investigate its status.RescheduledThis issue was previously scheduled to an earlier milestone

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions