Inline object do not throw type error

const merge = <S extends Record<string | number | symbol, unknown>>(source: S, target: S) => {...}
const merge = <S extends Record<string | number | symbol, unknown>>(source: S, target: S) => {...}
With the above function I would expect that typescript will complain when types do not match between the 2 arguments
const oneTwo = { one: '1', two: '1' };
const three = { three: '2' };

/*
Typescript throws the EXPECTED error:
Argument of type '{ three: string; }' is not assignable to parameter of type '{ one: string; two: string; }'.
*/
merge(oneTwo, three);

/*
Here Typescript does not throw the expected eror
*/
merge({ one: '1', two: '1' }, { three: '2' });
const oneTwo = { one: '1', two: '1' };
const three = { three: '2' };

/*
Typescript throws the EXPECTED error:
Argument of type '{ three: string; }' is not assignable to parameter of type '{ one: string; two: string; }'.
*/
merge(oneTwo, three);

/*
Here Typescript does not throw the expected eror
*/
merge({ one: '1', two: '1' }, { three: '2' });
I could not wrap my head around why this is happening
1 Reply
erik.gh
erik.gh2y ago
that happens because you are using constants in the first example which narrows the type further your last example is basically equal to:
let oneTwo_ = { one: '1', two: '1' };
let three_ = { three: '2' };

merge(oneTwo_, three_)
let oneTwo_ = { one: '1', two: '1' };
let three_ = { three: '2' };

merge(oneTwo_, three_)
which also does not throw an error
Want results from more Discord servers?
Add your server