I am trying to define a type where a field is mandatory only if there is another field (that is optional). I have tried a very simple case, but I can't understand why it doesn't work. I have noticed also that the same solution was suggested in this post.
interface A {
address: string
data?: string
}
interface B extends A {
nonce: number
callbackAddress?: string
}
export type Configuration = string | A | B
const conf: B = {
address: '',
callbackAddress: '',
nonce: 1 // if I try to remove this field, I get an error.
}
// this is the complete interface
const conf2: Configuration = { address: '', nonce: 123, callbackAddress: '0x' }
// I expect 'nonce' to be required here
const conf1: Configuration = { address: '', callbackAddress: '0x' }
Why doesn't the last line raise an error?