microsoft / microsoft/TypeScript
[3.9.2] Regression - combination of mapped type, intersection and generic produces error under infer
Offen
@weswigham arbeitet bereits daran.
Seit 05.6.2020.
Needs Investigation
Rescheduled
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 2 T. 4 Std.
- Gemergte PRs (30 T.)
- 132
Beschreibung
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:
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Bewertung
Dieses Issue wurde noch nicht bewertet.