I'm trying to type a function parameter where the param accepts any type where the union contains a known subset.
For example:
type One = { a: "one"; b: 1 };
type Two = { a: "two"; b: 2 };
type U = One | Two;
// `CouldContain` is some kind of wrapper that says it MIGHT contain `Two`
function test(param: CouldContain<Two>) {}
So the following would work:
const pass: U = { a: "one", b: 1 };
test(pass); // `U` contains `Two`
And so would:
type Z = { } | Two
const pass: Z = { };
test(pass); // `Z` contains `Two`
But this wouldn't:
const fail = { a: "three", b: 3}
test(fail); // Not inferred as `Two`
Nor would:
const fail = { };
test(fail); // Valid branch of `Z` which contains `Two`, but no way to infer this as `Z` so it fails
The use-case I have is inferring whether dynamically generated GraphQL data types might contain a specific union type.