2

I wanna do something like

type ObjectType = {
  'a' | 'b': number;
};

const obj1: ObjectType = { a: 0} // <- good  
const obj2: ObjectType = { b: 0} // <- good  
const obj3: ObjectType = { a: 0, b: 1 } // <- bad  

/// or 

type Union = 'a' | 'b';
type ObjectType = {
  [Union]: number;
};

const obj1: ObjectType = { a: 0} // <- good  
const obj2: ObjectType = { b: 0} // <- good  
const obj3: ObjectType = { a: 0, b: 1 } // <- bad  

Is it possible to do it? If so, how can I do it?

Abdul Niyas P M
  • 18,035
  • 2
  • 25
  • 46
Pytan
  • 1,138
  • 11
  • 28
  • Seems like you're asking about [mutually exclusive types](https://stackoverflow.com/questions/42123407/does-typescript-support-mutually-exclusive-types). – Robby Cornelissen Jan 27 '23 at 07:07
  • TS has a nice feature which is called [distributive conditional types](https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types) . See [this](https://tsplay.dev/NDDZjN) example. – captain-yossarian from Ukraine Jan 27 '23 at 08:30

0 Answers0