I encountered an issue where:
Pick<Foo, Bar> & Omit<Foo, Bar> !== Foo
This situation is perplexing because I anticipated Pick to choose keys in Bar and Omit to select keys not in Bar. However, when attempting this in TypeScript, an error arises:
export function bind<
TOpts extends Record<string, unknown>,
TReturn,
TBound extends keyof TOpts
>(
fn: (opts: TOpts) => TReturn,
bound: Pick<TOpts, TBound>
): (opts: Omit<TOpts, TBound>) => TReturn {
return (opts) => {
return fn({
...bound,
...opts,
});
};
}
Argument of type 'Pick<TOpts, TBound> & Omit<TOpts, TBound>' is not assignable to parameter of type 'TOpts'.
'Pick<TOpts, TBound> & Omit<TOpts, TBound>' is assignable to the constraint of type 'TOpts', but 'TOpts' could be instantiated with a different subtype of constraint 'Record<string, unknown>'.
Link to TypeScript playground: TypeScript Playground Link
I'm curious if this error is justified or simply an anomaly resulting from one of TypeScript's optimizations. Specifically, under what circumstances would the assertion not hold true for values of TOpts and TBound?