下面我有getEntityId并且我想创建一个包装函数,它可以在许多函数getOrFail中使用,这些函数接受一个函数并返回一个调用传入函数的函数,并在返回或抛出之前断言它是真的。
import * as _ from 'lodash'
export const getEntityId = (value: any, entity: string): number | null => {
if (typeof value === 'number') return value
if (_.get(value, 'id')) return value.id
if (_.get(value, `${entity}Id`)) return value.id
return null
}
export const getOrFail = <A, T> (fn: (...a: T[]) => A, message) => (...args: T[]) => {
const value = fn(...args)
if (value) return value
throw new Error(message);
}
export const getEntityIdOrFail = getOrFail(getEntityId, 'failed getting entity id')
我也试过这个:
export const getOrFail = (fn, message) => (...args: ArgumentTypes<typeof fn>): ReturnType<typeof fn> => {
const value = fn(...args)
if (value) return value
throw new Error(message);
}
我正在寻找一种使用泛型来制作它的方法,以便getEntityIdOrFail具有正确的类型信息。这怎么可能?
所以我需要明白三件事:
- 如何将
fn类型参数传递给(...args) - 如何传递
fn返回类型 - 如何
null从返回值中删除