Apologies for the vague title, I'm struggling to find the right words to explain this:
I have two interfaces A and B:
interface A {
prop1: string
prop2: object
}
interface B {
prop3: number
}
There is also interface C, which extends A:
interface C extends A {
prop2: {
objectProperty: string
}
}
Next, I have a function that returns objects of type A:
function func1(arg1: string, arg2: object): A {
return {
prop1: arg1,
prop2: arg2
}
}
And another function that returns objects of type B:
function func2(arg1: number): B {
return {
prop3: arg1
}
}
Finally, there is a function that should return objects of type C or B, created using the previous two functions:
function func3(): C | B {
if(...) {
return func1('some string', { objectProperty: 'another string' });
} else {
return func2(100);
}
}
My goal is to ensure that the returned value from the first branch aligns with type C and receive a warning if it doesn't. For instance:
return func1('some string', { someOtherProperty: 'another string' });
would trigger a warning as someOtherProperty is not part of type C.
I hope this explanation makes sense... I intend to streamline the process of constructing different types extending A with a single factory method while enforcing the correct output. It might seem like overkill, but I believe there should be a way to achieve this?