Только через "палку", для того она и нужна:
interface CreatureBase {
type: string;
subtype: string;
params: unknown;
}
interface PeopleBase extends CreatureBase {
type: 'People';
}
interface PeopleChild extends PeopleBase {
subtype: 'child';
params: {
age: number;
school: string;
}
}
interface PeopleAdult extends PeopleBase {
subtype: 'adult';
params: {
height: number,
weight: number,
age: number
}
}
type Creature = PeopleChild | PeopleAdult; //...
const arr: Creature[] = [
{
type: 'People',
subtype: 'adult',
params: {
height: 1,
weight: 2,
age: 3
}
},
{
type: 'People',
subtype: 'child',
params: {
school: 'foo',
age: 3
}
},
{
type: 'People',
subtype: 'adult',
params: {
school: 'foo', // err
age: 3
}
}
]
А обойти типизацию можно кастанув
as any
, или дважды кастанув в новый тип
var as unknown as type
. Но не нужно.)