I'm currently working on a function parameter that can accept any type within a union that includes a specified subset.
For instance:
type One = { a: "one"; b: 1 };
type Two = { a: "two"; b: 2 };
type U = One | Two;
// `CouldContain` is a wrapper indicating potential inclusion of `Two`
function test(param: CouldContain<Two>) {}
With this setup, the following examples would be valid:
const pass: U = { a: "one", b: 1 };
test(pass); // `U` contains `Two`
As well as:
type Z = { } | Two
const pass: Z = { };
test(pass); // `Z` contains `Two`
However, scenarios like these would not work:
const fail = { a: "three", b: 3}
test(fail); // Not recognized as `Two`
And neither would:
const fail = { };
test(fail); // Although part of `Z` which includes `Two`, it fails to infer as `Z`
The specific use I have in mind involves dynamically determining if GraphQL data types may contain a particular union type.