I have a function that looks like this:
function foo(a) {
if(a.optionalProperty === undefined) {
return {
one: 1,
two: 2
}
} else {
return {
one: 1,
two: 2,
optionalResultProperty: "some stuff"
}
}
}
The parameter a is one of the following two types:
SomeType & {
optionalProperty: string
}
// or
SomeType
I would like to specify the return type so that it matches the function definition, meaning when the optionalProperty is present, the return type should be a certain return type and if it is not present, it should be a different return type.
This is what I have tried so far:
function foo<x extends (SomeType & { optionalProperty: string }) | SomeType>(a: x): x extends (SomeType & { optionalProperty: string }) ? SomeReturnType : Omit<SomeReturnType, "optionalResultProperty"> {
// ..
}
However, this doesn't seem to be correct. It says that the code is not assignable to the type.
What would be the correct way to do this?