Type to require elements of array to all have one property or the other

I'm looking for a type that will force all elements of the array to have 1 property or the other example
interface ColumnCommon {
id: string;
}


type WidthedColumn = ColumnCommon & { width?: Length; };
type WeightedColumn = ColumnCommon & { weight?: number; };

type Columns = Array<WidthedColumn> | Array<WeightedColumn>
interface ColumnCommon {
id: string;
}


type WidthedColumn = ColumnCommon & { width?: Length; };
type WeightedColumn = ColumnCommon & { weight?: number; };

type Columns = Array<WidthedColumn> | Array<WeightedColumn>
so here I need two things 1. one column can't have both weight and width 2. all columns in the array should have the same property (weight or width)
3 Replies
Scot
Scot2y ago
does the above not work? hmm
julius
julius2y ago
you need to assign the other prop as never
julius
julius2y ago
TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
Want results from more Discord servers?
Add your server