'Cannot find name 'apply'.ts(2304) on function
I cannot persuade TS to use apply method on function :/
Using merge from lodash
merge<PropertyValidationList, PropertyValidationList>.apply(merge, lists);
type definition of merge
merge<TObject, TSource>(object: TObject, source: TSource): TObject & TSource;
merge<TObject, TSource1, TSource2>(object: TObject, source1: TSource1, source2: TSource2): TObject & TSource1 & TSource2;
etc..
getting error:
Cannot find name 'apply'.ts(2304)
I've tryied:
(merge<PropertyValidationList, PropertyValidationList>).apply(merge, lists);
(merge as typeof merge<PropertyValidationList, PropertyValidationList>).apply(merge, lists);
merge.apply<PropertyValidationList, PropertyValidationList>(merge, lists);
etc..
googled stuff.. nothing helped
Bypassed for now with ugly fix
const propertyList: PropertyValidationList = (merge as any).apply(merge, lists);
interface LoDashStatic {
/**
* Recursively merges own and inherited enumerable properties of source
* objects into the destination object, skipping source properties that resolve
* to `undefined`. Array and plain object properties are merged recursively.
* Other objects and value types are overridden by assignment. Source objects
* are applied from left to right. Subsequent sources overwrite property
* assignments of previous sources.
*
* **Note:** This method mutates `object`.
*
* @category Object
* @param object The destination object.
* @param [sources] The source objects.
* @returns Returns `object`.
* @example
*
* var users = {
* 'data': [{ 'user': 'barney' }, { 'user': 'fred' }]
* };
*
* var ages = {
* 'data': [{ 'age': 36 }, { 'age': 40 }]
* };
*
* _.merge(users, ages);
* // => { 'data': [{ 'user': 'barney', 'age': 36 }, { 'user': 'fred', 'age': 40 }] }
*/
merge<TObject, TSource>(object: TObject, source: TSource): TObject & TSource;
/**
* @see _.merge
*/
merge<TObject, TSource1, TSource2>(object: TObject, source1: TSource1, source2: TSource2): TObject & TSource1 & TSource2;
/**
* @see _.merge
*/
merge<TObject, TSource1, TSource2, TSource3>(object: TObject, source1: TSource1, source2: TSource2, source3: TSource3): TObject & TSource1 & TSource2 & TSource3;
/**
* @see _.merge
*/
merge<TObject, TSource1, TSource2, TSource3, TSource4>(object: TObject, source1: TSource1, source2: TSource2, source3: TSource3, source4: TSource4): TObject & TSource1 & TSource2 & TSource3 & TSource4;
/**
* @see _.merge
*/
merge(object: any, ...otherArgs: any[]): any;
};
const merge: LoDashStatic['merge'] = (...args: Object[]) => {
// faking implementation
const result = {};
args.forEach(obj => Object.assign(result, obj));
return result;
}
export type ValidatorSync = (val: any) => true | string;
export type ValidationAsync = Promise<true | string>;
export type ValidatorAsync = (val: any) => ValidationAsync;
export type Validator = ValidatorSync | ValidatorAsync;
export type ValidationResponse = { valid: boolean };
export type PropertyValidation = { rules: Validator[], restriction: string };
export type PropertyValidationList = Record<string, Partial<PropertyValidation>>;
export type PropertyValidationLists = PropertyValidationList | PropertyValidationList[];
export function propertyHash(property: string, ...additinalParams: string[]): string {
return [property, ...additinalParams].reverse().join('_');
}
export function propertyValidation(lists: PropertyValidationLists, ...args: Parameters<typeof propertyHash>): PropertyValidation {
lists = (lists instanceof Array) ? lists : [lists];
// const propertyList = merge<PropertyValidationList, ...PropertyValidationList>.apply(merge, lists);
const propertyList = merge(...lists);
return {
rules: [],
restriction: '',
...propertyList[propertyHash(...args)]
};
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
