Open
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?