Replace EnumValue type by more general TypeOf type.

This commit is contained in:
baldo 2022-08-25 21:10:37 +02:00
parent 59ef8256e6
commit a1553ac0bf
2 changed files with 9 additions and 14 deletions

View file

@ -1,9 +1,4 @@
import {
ArrayField,
Field,
RawJsonField,
registerStringMapper,
} from "sparkson";
import { ArrayField, Field, RawJsonField } from "sparkson";
// Types shared with the client.
export type TypeGuard<T> = (arg: unknown) => arg is T;
@ -71,8 +66,8 @@ export type JSONArray = Array<JSONValue>;
export const isJSONArray = toIsArray(isJSONValue);
export type EnumValue<E> = E[keyof E];
export type EnumTypeGuard<E> = TypeGuard<EnumValue<E>>;
export type ValueOf<T> = T[keyof T];
export type EnumTypeGuard<E> = TypeGuard<ValueOf<E>>;
export function unhandledEnumField(field: never): never {
throw new Error(`Unhandled enum field: ${field}`);
@ -138,7 +133,7 @@ export function isBoolean(arg: unknown): arg is boolean {
}
export function isUndefined(arg: unknown): arg is undefined {
return arg === undefined;
return typeof arg === "undefined";
}
export function isNull(arg: unknown): arg is null {
@ -150,7 +145,7 @@ export function toIsArray<T>(isT: TypeGuard<T>): TypeGuard<T[]> {
}
export function toIsEnum<E>(enumDef: E): EnumTypeGuard<E> {
return (arg): arg is EnumValue<E> =>
return (arg): arg is ValueOf<E> =>
Object.values(enumDef).includes(arg as [keyof E]);
}

View file

@ -12,7 +12,7 @@ import {
import { Request, Response } from "express";
import {
EnumTypeGuard,
EnumValue,
ValueOf,
type GenericSortField,
getFieldIfExists,
isJSONObject,
@ -79,10 +79,10 @@ function respond(
function orderByClause<S>(
restParams: RestParams,
defaultSortField: EnumValue<S>,
defaultSortField: ValueOf<S>,
isSortField: EnumTypeGuard<S>
): OrderByClause {
let sortField: EnumValue<S> | undefined = isSortField(restParams._sortField)
let sortField: ValueOf<S> | undefined = isSortField(restParams._sortField)
? restParams._sortField
: undefined;
if (!sortField) {
@ -347,7 +347,7 @@ export { filterCondition as whereCondition };
export function filterClause<S>(
restParams: RestParams,
defaultSortField: EnumValue<S>,
defaultSortField: ValueOf<S>,
isSortField: EnumTypeGuard<S>,
filterFields: string[]
): FilterClause {