microsoft / microsoft/TypeScript
[3.9.2] Regression - combination of mapped type, intersection and generic produces error under infer
Aperta
@weswigham ci sta già lavorando.
Dal 5/6/2020.
Needs Investigation
Rescheduled
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
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:
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Valutazione
Questa issue non è ancora stata valutata.