Given a function which receives two parameters of the same generic type:
declare const fn: <T>(t1: T, t2: T) => any;
I am trying to reason about the different behaviour TypeScript has when this function is invoked.
When the 2 parameters are both different primitives:
fn(1, 'foo'); // Error due to different types
When the 2 parameters are both different objects:
fn({ foo: 1 }, { bar: 1 }) // No error and return type is union of different types
How come these two usages don't have the same behaviour? I would expect them both to behave the same. Either:
- Error due to different types
- No error and return type is union of different types
Secondly, TypeScript's behaviour is different again if one of the parameters passed in is a variable (rather than an inline object literal):
fn({ foo: 1 }, { bar: 1 }) // No error and return type is union of different types
const obj = { foo: 1 };
fn(obj, { bar: 1 }) // Error due to different types
Again, how come these two usages don't have the same behaviour? I would expect both of these cases to behave the same.