I would like to compute an union from the existing union using literals in array.
Is it possible?
Example
interface Intf1 {
type: "one";
data: {
param: boolean;
};
}
interface Intf2 {
type: "two";
data: string;
}
interface Intf3 {
type: "three";
data: {
param: number;
};
}
const registered = ["one", "two"] as const;
type CombinedIntf = Intf1 | Intf2 | Intf3;
type CustomIntersect = ?
const getObj = () => {
return {} as any; // impl
};
const obj: CustomIntersect<CombinedIntf, typeof registered> = getObj();
// Intf1 | Intf2
switch(obj.type) {
case "one":
console.log(obj.data.param);
break;
case "two":
console.log(obj.param);
break;
case "three": // should give an error ""three" is not comparable to type "one", "two""
console.log(obj.param);
break;
}
Thank you!