Replace EnumValue type by more general TypeOf type.
This commit is contained in:
parent
59ef8256e6
commit
a1553ac0bf
|
@ -1,9 +1,4 @@
|
||||||
import {
|
import { ArrayField, Field, RawJsonField } from "sparkson";
|
||||||
ArrayField,
|
|
||||||
Field,
|
|
||||||
RawJsonField,
|
|
||||||
registerStringMapper,
|
|
||||||
} from "sparkson";
|
|
||||||
|
|
||||||
// Types shared with the client.
|
// Types shared with the client.
|
||||||
export type TypeGuard<T> = (arg: unknown) => arg is T;
|
export type TypeGuard<T> = (arg: unknown) => arg is T;
|
||||||
|
@ -71,8 +66,8 @@ export type JSONArray = Array<JSONValue>;
|
||||||
|
|
||||||
export const isJSONArray = toIsArray(isJSONValue);
|
export const isJSONArray = toIsArray(isJSONValue);
|
||||||
|
|
||||||
export type EnumValue<E> = E[keyof E];
|
export type ValueOf<T> = T[keyof T];
|
||||||
export type EnumTypeGuard<E> = TypeGuard<EnumValue<E>>;
|
export type EnumTypeGuard<E> = TypeGuard<ValueOf<E>>;
|
||||||
|
|
||||||
export function unhandledEnumField(field: never): never {
|
export function unhandledEnumField(field: never): never {
|
||||||
throw new Error(`Unhandled enum field: ${field}`);
|
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 {
|
export function isUndefined(arg: unknown): arg is undefined {
|
||||||
return arg === undefined;
|
return typeof arg === "undefined";
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isNull(arg: unknown): arg is null {
|
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> {
|
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]);
|
Object.values(enumDef).includes(arg as [keyof E]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@ import {
|
||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
import {
|
import {
|
||||||
EnumTypeGuard,
|
EnumTypeGuard,
|
||||||
EnumValue,
|
ValueOf,
|
||||||
type GenericSortField,
|
type GenericSortField,
|
||||||
getFieldIfExists,
|
getFieldIfExists,
|
||||||
isJSONObject,
|
isJSONObject,
|
||||||
|
@ -79,10 +79,10 @@ function respond(
|
||||||
|
|
||||||
function orderByClause<S>(
|
function orderByClause<S>(
|
||||||
restParams: RestParams,
|
restParams: RestParams,
|
||||||
defaultSortField: EnumValue<S>,
|
defaultSortField: ValueOf<S>,
|
||||||
isSortField: EnumTypeGuard<S>
|
isSortField: EnumTypeGuard<S>
|
||||||
): OrderByClause {
|
): OrderByClause {
|
||||||
let sortField: EnumValue<S> | undefined = isSortField(restParams._sortField)
|
let sortField: ValueOf<S> | undefined = isSortField(restParams._sortField)
|
||||||
? restParams._sortField
|
? restParams._sortField
|
||||||
: undefined;
|
: undefined;
|
||||||
if (!sortField) {
|
if (!sortField) {
|
||||||
|
@ -347,7 +347,7 @@ export { filterCondition as whereCondition };
|
||||||
|
|
||||||
export function filterClause<S>(
|
export function filterClause<S>(
|
||||||
restParams: RestParams,
|
restParams: RestParams,
|
||||||
defaultSortField: EnumValue<S>,
|
defaultSortField: ValueOf<S>,
|
||||||
isSortField: EnumTypeGuard<S>,
|
isSortField: EnumTypeGuard<S>,
|
||||||
filterFields: string[]
|
filterFields: string[]
|
||||||
): FilterClause {
|
): FilterClause {
|
||||||
|
|
Loading…
Reference in a new issue