Type Alias InferParamsFromSemantics<TSemantics>

InferParamsFromSemantics<TSemantics>: TSemantics extends readonly [infer TField extends ReadonlyDeep<H5PFieldWithoutLabel>, ...(infer TRestFields extends ReadonlyArray<ReadonlyDeep<H5PFieldWithoutLabel>>)]
    ? (TField extends ReadonlyDeep<L10nGroupWithoutLabel>
        ? InferL10nType<TField>
        : TField extends {
                optional: true;
            }
            ? TField extends {
                    default: FieldToParamType<TField>;
                }
                ? Record<TField["name"], InferOptionalWithDefault<TField>>
                : Partial<Record<TField["name"], InferOptionalWithDefault<TField>>>
            : TField extends ReadonlyDeep<Omit<H5PFieldGroupWithoutLabel, "fields"> & {
                    fields: [infer InnerField extends (...)];
                }>
                ? InnerField extends {
                        optional: true;
                    }
                    ? InnerField extends {
                            default: FieldToParamType<TField>;
                        }
                        ? Record<TField["name"], InferOptionalWithDefault<TField>>
                        : Partial<Record<(...)[(...)], InferOptionalWithDefault<(...)>>>
                    : Record<TField["name"], InferOptionalWithDefault<TField>>
                : Record<TField["name"], InferOptionalWithDefault<TField>>) & InferParamsFromSemantics<TRestFields>
    : unknown

Type Parameters

  • TSemantics extends ReadonlyArray<ReadonlyDeep<H5PFieldWithoutLabel>>

    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.

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 };