1

I would like a type to enforce the following

given:

interface Foo {
    key1: bool;
    key2: string;
    ...: 
    keyn: <whatever>;
}

I'd like something like:

OnlyOnePropery<Foo>

where only following would be accepted for the type:

  • { key1: bool;} xor {key2: string;} xor ... xor {keyn: <whatever>;}

So basically one and only one key value pair.

byrnedo
  • 1,415
  • 10
  • 12
  • 1
    Sorry I misunderstood the question in the comment. I think the original answer works fine for this use case too: https://www.typescriptlang.org/play/#code/C4TwDgpgBAoglsAFhATgMThANgEwDwAqANFAQNIQhQQAewEAdjgM5QDWlA9gGalQC87LrwIA+AQCgo00hSq16TVhxA8+AfigBvKAG0AClDgNZlALoBadQC4Cu8uagBfKADIo+gIYpgcT1jwAJQgAY04UfBgaEKwAVxwIPBU1YlMQURIGCAA3VFFRaygs3JQJCWN6FG5PEOg0Tk5tKRkVAEZCgCMGrAhPBgBuZukVACZC5mAUYwBzQZkoIaEQBkKAEU96Qacy0EhYBGR0BoF9pFQMbHx6zlEJIA – Titian Cernicova-Dragomir Aug 01 '19 at 11:10
  • ok silly me, I thought I checked your previous and that my tsc ignored the 3rd + property... – byrnedo Aug 01 '19 at 11:16
  • You mind if I mark it as duplicate ? Or will you delete the question ? – Titian Cernicova-Dragomir Aug 01 '19 at 11:18
  • go ahead and mark as dup I think – byrnedo Aug 01 '19 at 11:26

0 Answers0