Getting typescript to throw an error on an instance of a union type which contains a set of properties inconsistent with every element of the union

Viewed 30

Suppose that I have

type TYPE1 = { 
    id : number, 
    property1 : number 
}

type TYPE2 = { 
    id : number, 
    property2 : number
}

and

type T = TYPE1 | TYPE2;

Is there a way of having typescript provide a warning or error if something like

const t: T = { 
    id : 0, 
    property1 : 0, 
    property2 : 0
}

appears elsewhere in the code as it is neither of TYPE1 nor TYPE2.

0 Answers
Related