Is there a way to use an enum to define the valid types that an array can contain? I have been unable to find a solution so far, and I am curious if it is feasible.
Below is the example code I have tried:
interface User {
name: string;
}
interface Admin {
level: number;
}
enum Person {
User,
Admin,
}
const persons: Person[] = [
{ name: 'hello' },
{ level: 1 },
];
If you're wondering about achieving something similar with a union type, here's how it could be done:
interface User {
name: string;
}
interface Admin {
level: number;
}
type Person = User | Admin;
const persons: Person[] = [
{ name: 'hello' },
{ level: 1 },
];