-
Notifications
You must be signed in to change notification settings - Fork 13k
Open
Labels
Domain: Conditional TypesThe issue relates to conditional typesThe issue relates to conditional typesExperience EnhancementNoncontroversial enhancementsNoncontroversial enhancementsSuggestionAn idea for TypeScriptAn idea for TypeScript
Milestone
Description
TypeScript Version: 3.2.1
Search Terms:
3.2.1
extends
intersection generic
Code
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
type Func<T> = (arg: T) => null
type Context = 'Context';
export function withRouteContextPropConsumer<
T extends { routeContext: Context }
>(
funcToWrap: Func<T>,
): Func<Omit<T, "routeContext">> {
return (args: Omit<T, "routeContext">) => {
const routeContext: Context = 'Context';
return funcToWrap({ ...args, routeContext });
};
}
Expected behavior:
Code compiles without errors
Actual behavior:
Argument of type '{ routeContext: "Context"; }' is not assignable to parameter of type 'T'.
After upgrading from 3.0.3 to 3.2.1, it seems that tsc has (at least partially) lost the ability to reason about constrained generics.
In the example above (one of our React context helper functions, modified to remove the React dependency), the function is parameterised over a constrained generic:
T extends { routeContext: Context }
But a few lines later, the compiler complains that the generic T
may not have a routeContext
attribute. T must have a routeContext
attribute however, because of the constraint. Perhaps the Omit
helper is confusing things?
shiba-codes, zheeeng, shir, apexskier, DullReferenceException and 19 more
Metadata
Metadata
Assignees
Labels
Domain: Conditional TypesThe issue relates to conditional typesThe issue relates to conditional typesExperience EnhancementNoncontroversial enhancementsNoncontroversial enhancementsSuggestionAn idea for TypeScriptAn idea for TypeScript