Infer the params type from a semantics array.
⚠️ Use with caution - If the semantics form has very many fields, this might not work. In that case, please create an issue to let us know.
const semantics = [
{
label: "Group",
name: "group",
type: "group",
fields: [
{
label: "Field",
name: "field1",
type: "number",
},
{
label: "Field",
name: "field2",
type: "boolean",
default: false,
},
],
},
] as const;
type Params = InferParamsFromSemantics<typeof semantics>;
// ^^^^^^ { group: { field1: number; field2: boolean } };
If a group only has one field, it will be inferred as the type of the field:
const semantics = [
{
label: "Group",
name: "group",
type: "group",
fields: [
{
label: "Field",
name: "field1",
type: "number",
},
],
},
] as const;
type Params = InferParamsFromSemantics<typeof semantics>;
// ^^^^^^ { group: number };
The semantics array type.
This type is recursive and should cover all cases of semantics arrays. It is a nice way of inferring the type of all fields in a semantics array, and will take care of weird cases like groups with only one field in them.