microsoft / microsoft/TypeScript
[3.9.2] Regression - combination of mapped type, intersection and generic produces error under infer
Open
@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.
Related Issues:
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.