Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use a shortcut if the field name is "l10n" #126

Merged
merged 2 commits into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 76 additions & 69 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ export type H5PEditorObject<
addCommonField: <TField extends H5PField>(
field: TField,
parent: H5PForm,
params: ParamTypeInferredFromFieldType<TField>,
params: InferParamTypeFromFieldType<TField>,
ancestor: H5PForm,
skipAppendTo?: boolean,
) => void;
Expand Down Expand Up @@ -598,7 +598,7 @@ export type H5PEditorObject<
followField: <TField extends H5PField = H5PField>(
parent: H5PForm,
path: string,
callback: (params: ParamTypeInferredFromFieldType<TField>) => void,
callback: (params: InferParamTypeFromFieldType<TField>) => void,
) => void;
/**
* Helps generating a consistent description ID across fields
Expand Down Expand Up @@ -716,7 +716,7 @@ export type H5PEditorObject<
semanticsChunk: TFields,
params: Record<
TFields[number]["name"],
ParamTypeInferredFromFieldType<TFields[number]>
InferParamTypeFromFieldType<TFields[number]>
>,
$wrapper: JQuery<HTMLElement>,
parent: H5PForm,
Expand Down Expand Up @@ -1972,7 +1972,7 @@ type InferGroupWithOneFieldParams<
TGroupField extends DeepReadonly<H5PFieldGroup>,
> = TGroupField["fields"][0] extends H5PFieldGroup
? InferGroupParams<DeepReadonly<TGroupField["fields"][0]>>
: ParamTypeInferredFromFieldType<TGroupField["fields"][0]>;
: InferParamTypeFromFieldType<TGroupField["fields"][0]>;
/**
* If there are two ore more fields in the group,
* the group's params is an object where the field's name is the key,
Expand All @@ -1992,7 +1992,7 @@ export type InferParamsType<TField extends DeepReadonly<H5PField>> =
? InferGroupParams<TField>
: TField extends DeepReadonly<H5PFieldList>
? Array<InferParamsType<TField["field"]>>
: ParamTypeInferredFromFieldType<TField>;
: InferParamTypeFromFieldType<TField>;
/**
* ⚠️ Use with caution - if the semantics form has many fields, this might not work ⚠️
* Infer the params type from a semantics array.
Expand Down Expand Up @@ -2030,15 +2030,17 @@ export type InferParamsFromSemantics<
> = TSemantics extends readonly [infer TField, ...infer TRestFields]
? TField extends DeepReadonly<H5PField>
? TRestFields extends ReadonlyArray<DeepReadonly<H5PField>>
? Record<
TField["name"],
TField["optional"] extends true
? // eslint-disable-next-line @typescript-eslint/ban-types
TField extends { default: {} }
? InferParamsType<TField>
: InferParamsType<TField> | undefined
: InferParamsType<TField>
> &
? (TField extends DeepReadonly<H5PFieldGroup & { name: "l10n" }>
? Record<"l10n", Record<TField["fields"][number]["name"], string>>
: Record<
TField["name"],
TField["optional"] extends true
? // eslint-disable-next-line @typescript-eslint/ban-types
TField extends { default: {} }
? InferParamsType<TField>
: InferParamsType<TField> | undefined
: InferParamsType<TField>
>) &
InferParamsFromSemantics<TRestFields>
: unknown
: unknown
Expand All @@ -2050,63 +2052,68 @@ export type Media = {
copyright?: Copyright;
};

export type InferParamTypeFromFieldType<TField extends DeepReadonly<H5PField>> =
TField extends DeepReadonly<H5PFieldAudio>
? TField["optional"] extends true
? Audio | undefined
: Audio
: TField extends DeepReadonly<H5PFieldBoolean>
? TField["optional"] extends true
? TField extends { default: boolean }
? boolean
: boolean | undefined
: boolean
: TField extends DeepReadonly<H5PFieldFile>
? TField["optional"] extends true
? Media | undefined
: Media
: TField extends DeepReadonly<H5PFieldGroup>
? TField["optional"] extends true
? InferParamsType<TField> | undefined
: InferParamsType<TField>
: TField extends DeepReadonly<H5PFieldImage>
? TField["optional"] extends true
? Image | undefined
: Image
: TField extends DeepReadonly<H5PFieldLibrary>
? TField["optional"] extends true
? TField extends { default: unknown }
? unknown
: unknown | undefined
: unknown
: TField extends DeepReadonly<H5PFieldList>
? TField["optional"] extends true
? Array<InferParamsType<TField["field"]>> | undefined
: Array<InferParamsType<TField["field"]>>
: TField extends DeepReadonly<H5PFieldNumber>
? TField["optional"] extends true
? TField extends { default: number }
? number
: number | undefined
: number
: TField extends DeepReadonly<H5PFieldSelect>
? TField["optional"] extends true
? TField extends { default: string | number | boolean }
? TField["options"][number]["value"]
: TField["options"][number]["value"] | undefined
: TField["options"][number]["value"]
: TField extends DeepReadonly<H5PFieldText>
? TField["optional"] extends true
? TField extends { default: string }
? string
: string | undefined
: string
: TField extends DeepReadonly<H5PFieldVideo>
? TField["optional"] extends true
? Video | undefined
: Video
: never;
/**
* @deprecated Use InferParamTypeFromFieldType instead
*/
export type ParamTypeInferredFromFieldType<
TField extends DeepReadonly<H5PField>,
> = TField extends DeepReadonly<H5PFieldAudio>
? TField["optional"] extends true
? Audio | undefined
: Audio
: TField extends DeepReadonly<H5PFieldBoolean>
? TField["optional"] extends true
? TField extends { default: boolean }
? boolean
: boolean | undefined
: boolean
: TField extends DeepReadonly<H5PFieldFile>
? TField["optional"] extends true
? Media | undefined
: Media
: TField extends DeepReadonly<H5PFieldGroup>
? TField["optional"] extends true
? InferParamsType<TField> | undefined
: InferParamsType<TField>
: TField extends DeepReadonly<H5PFieldImage>
? TField["optional"] extends true
? Image | undefined
: Image
: TField extends DeepReadonly<H5PFieldLibrary>
? TField["optional"] extends true
? TField extends { default: unknown }
? unknown
: unknown | undefined
: unknown
: TField extends DeepReadonly<H5PFieldList>
? TField["optional"] extends true
? Array<InferParamsType<TField["field"]>> | undefined
: Array<InferParamsType<TField["field"]>>
: TField extends DeepReadonly<H5PFieldNumber>
? TField["optional"] extends true
? TField extends { default: number }
? number
: number | undefined
: number
: TField extends DeepReadonly<H5PFieldSelect>
? TField["optional"] extends true
? TField extends { default: string | number | boolean }
? TField["options"][number]["value"]
: TField["options"][number]["value"] | undefined
: TField["options"][number]["value"]
: TField extends DeepReadonly<H5PFieldText>
? TField["optional"] extends true
? TField extends { default: string }
? string
: string | undefined
: string
: TField extends DeepReadonly<H5PFieldVideo>
? TField["optional"] extends true
? Video | undefined
: Video
: never;
> = InferParamTypeFromFieldType<TField>;

export type Video = Media;

Expand Down
1 change: 0 additions & 1 deletion index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading