I have different types with numeric id
properties, and I want to make sure I don't pass the wrong id into functions (the numbers are only unique within the type).
Is there a way to make typescript complain if I pass a wrong id type?
type AId = number
type A = {id: AId}
type BId = number
type B = {id: BId}
const fnA = (id: AId) => {}
const b: BId = {id: 5}
// I'd like for this to give a type error (not a runtime check)
fnA(b.id)
This code does not give a type warning. Can TS be told that only AId
is the correct parameter type, and not just any type that is number
?