How do I create an array type that must contain all object property values (in any order) based on object definition?
Having an object type definition like this:
type ExampleObject = {
property1: "abc",
property2: "xyz",
property3: "123"
}
How do I define type that is satisifed only by these values (with items in any order):
const exeampleArray = ["abc", "xyz", "123"]
const exeampleArray2 = ["xyz", "abc", "123"]
... etc
My exact scenario is a little more complicated but it might be useful to know:
This is a definition of one TableColumn
export type TableColumn<
RowEntity extends object,
ColumnKey extends string,
ValueType,
> = {
getValue: (row: RowEntity) => ValueType;
key: ColumnKey;
};
This is a definition of table structure (column keys and their types):
export type TableStructure = {
name: string;
description: string;
active: boolean;
};
Now I want to define type, which will require array of all TableColumns based on TableStructure. I was able to create a definition of object containing all TableStructure keys with corresponding TableColumn
type TableColumnsObject<
TableStructure extends object,
RowEntity extends object,
> = {
[ColumnKey in keyof TableStructure]: TableColumn<
RowEntity,
Extract<ColumnKey, string>,
TableStructure[ColumnKey]
>;
};
But I now want to use have the columns in a form of an array like this:
const columns = [
TableColumn<RowEntity, "name", string>,
TableColumn<RowEntity, "description", string>,
TableColumn<RowEntity, "active", boolean>
]
I am sure there must be a way to do that, but don't know how.
Again - the columns may be in any order, I just need the type to check there are all TableColumn items according to TableStructure.
columnsarray is the argument of a function, then you may be able to use a validator type to check it rather than generating the tuple type upfront. If that is the case, please edit your question to say as much.{a: 0, b: 0, c: 1}. Personally I'd strongly recommend starting with an array of entry types and then compute both the value array and the obj type from it.