You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a distinct set of operations, represented as a a string literal Operation. Some operations require additional data/props, so I would like to create a type, that based on whether the array contains a certain operation, includes these additional properties.
I'm not sure if this is even possible to express in Typescript.
What I have so far (not working pseudo-code):
typeOperator="A"|"B"|"C"|"D";typeStructWithAorD={operators: Operator[];propOnlyForAorD: string;};typeStructWithB={operators: Operator[];propOnlyForB: string;};typeStructWithC={operators: Operator[];};typeStruct=StructWithAorD|StructWithB|StructWithC;declareconsta: Struct={operators: ["A"],propOnlyForAorD: "",// this should be mandatory if and only if operators array includes "A" one or several times (regardless of order)};declareconstb: Struct={operators: ["B"],propOnlyForB: "",// this should be mandatory if and only if operators array includes "B" one or several times (regardless of order)};declareconstaAndB: Struct={operators: ["A","B"],propOnlyForAorD: "",propOnlyForB: "",};declareconststructs: Struct[];
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I have a distinct set of operations, represented as a a string literal
Operation
. Some operations require additional data/props, so I would like to create a type, that based on whether the array contains a certain operation, includes these additional properties.I'm not sure if this is even possible to express in Typescript.
What I have so far (not working pseudo-code):
Beta Was this translation helpful? Give feedback.
All reactions