0

I used this method but it also accepts both interfaces. I need one to exclude the other.

export interface C extends A {
    single: boolean;
}

export interface B extends A {
    couple: boolean;
}

export interface A {
    others: string;
}

export type A = B | C;

Those are my test, in the third case i need that ts give me error:

\\ OK
const test1: A = {
    single: true,
    others: ''
}

\\ OK
const test2: A = {
    couple: true,
    others: ''
}

\\ OK BUT I DONT WANT THIS!!
const test3: A = {
    single: true,
    couple: true,
    others: ''
}
Alessandro_Russo
  • 1,799
  • 21
  • 34
  • This does not compile because the identifier `A` is used for both the interface and the union type. Apart from that there are syntax errors on the semicolons. – Rengers Aug 01 '19 at 10:47
  • 1
    Define `A` as `export type A = StrictUnion` and it will work, `StrictUnion` in the duplicate (would not mind if you upvote it ;)) Let me know if you have any issues with it, but i tested your example and it works – Titian Cernicova-Dragomir Aug 01 '19 at 10:52
  • 1
    Seems it's fixed in v3.5: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-5.html#improved-excess-property-checks-in-union-types – abuduba Aug 05 '19 at 12:23

0 Answers0