I want to have a component Foo
that accepts two types of props:
Foo({a: 'a'})
Foo({a: 'a', b: 'b', c:'c'})
where {a: 'a'}
is required.
These should invalid
Foo({a: 'a', b: 'b'}) // ❌
Foo({a: 'a', c: 'c'}) // ❌
Here is my attempt.
type BaseProps = {
a: "a";
};
type VariantPrps = {
b: "b";
c: "c";
} & BaseProps;
function Foo(props: BaseProps): React.ReactNode;
function Foo(props: VariantPrps): React.ReactNode;
function Foo(props: BaseProps | VariantPrps) {
// I can only access `props.a` inside
return <span>{props.a}</span>;
}
// usage
Foo({a: 'a'}) // ✅
Foo({a: 'a', b: 'b', c:'c'}) // ✅
Foo({a: 'a', b: 'b'}) // ❌
Foo({a: 'a', c: 'c'}) // ❌
it works to some extent but inside Foo
I can only access a
, not b
and c
. My intention is that inside that component I should be able to access b
and c
and check to see if they exist or not before using them. Is there a way to do that?