Why does Typescript's inference seemingly ignore the mismatch in types in this code snippet?
type MyType<TRecord extends Record<string,any>> = {
rec: TRecord
rec2: TRecord
}
const myRec = { idFoo: 3 }
function createMyType<T extends MyType<Record<string,any>>>(obj: T):T {
return obj
}
const myType = createMyType({
rec: myRec,
rec2: 3, // <-- error here as expected
})
const myType2 = createMyType({
rec: myRec,
rec2: {}, // <-- no error here
})
Is there a way to adjust the typing of MyType
or createMyType
so that it correctly flags the discrepancy between rec
and rec2
not having the same type (TRecord
)?
Clarification
In essence, how can we modify the types in this scenario to catch the type mismatch between rec
and rec2
?