I'm building a React component that would have a different HTML tag based on a property a user passed. Here's an example:
interface Common {
common?: number;
}
interface A extends Common {
first?: string;
second?: string;
check: true;
}
interface B extends Common {
third?: string;
fourth?: string;
check?: false;
}
type Test = A | B;
To test this, I'm expecting the following:
// Success
let test: Test = {
common: 1,
check: true,
first: "text"
}
// Success
let test: Test = {
common: 1,
check: false,
third: "text"
}
// Fail
let test: Test = {
common: 1,
check: true,
third: "text"
}
// Fail
let test: Test = {
common: 1,
check: false,
first: "text"
}
All of that is working, the challenge is to get type B without passing a value to check at all, like such:
let test: Test = {
common: 1,
first: "text", // should highlight an error because `check` is not passed
third: "text",
}
Here is what I tried:
// Attempt 1: is to include possible values
interface B extends Common {
third?: string;
fourth?: string;
check?: false | null | undefined | never | unknown | void; // tried them all
}
// Attempt 2: is not to include at all. Still didn't work
interface B extends Common {
third?: string;
fourth?: string;
}
testobj conforms to to B interface.check?: falseyou can do:check: false,to distinguish between those two interfaces.trueorfalseto thecheck. But the problem that I'm facing is that I want to infer that's it is of type B when I don't even pass the value ofcheck. I tried this:check: false | undefinedas in the value is not passed so it's undefined. But it didn't work