Open
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.
Related Issues: