Here is my current interface:
MyInterface {
prop1?: string,
prop2?: string,
}
Now, I am looking to introduce an alternative
property that mirrors the content of the existing properties but also infers if any properties are defined.
For instance:
const example: MyInterface = {
prop2: 'some value',
alternative: {
prop2: 'some alternative'
}
}
// Here, alternative is inferred as {prop2: string}
const example2: MyInterface = {
prop2: 'some value',
alternative: {} // ERROR! prop2 should be defined
}
// Here, alternative is inferred as {prop1: string}
const example3: MyInterface = {
prop1: 'some value',
alternative: {
prop2: 'this is prop 2'.
} // ERROR! prop1 should be defined
}
Is there a way to achieve this?
I attempted using typeof this
, but it was unsuccessful.