export enum IsDictionaryOfErrorCode { IS_NULL = "IS_NULL", IS_UNDEFINED = "IS_UNDEFINED", ELEMENT_FAILED_PREDICATE = "ELEMENT_FAILED_PREDICATE" } export type ElementPredicate = ((element : any) => (undefined|ErrorCodeT))|((element : any) => boolean); /* TODO Replace `any` with proper constraints */ export type IsDictionaryOfResult> = ( IsDictionaryOfErrorCode.IS_NULL | IsDictionaryOfErrorCode.IS_UNDEFINED | { index? : number|string, argIndex? : number|string, error : IsDictionaryOfErrorCode.ELEMENT_FAILED_PREDICATE } | { index? : number|string, argIndex? : number|string, error : ErrorCodeT } ) //ErrorCodeT should be an enum export function checkDictionaryOf (mixed : any, elementPredicate : ElementPredicate) : undefined|IsDictionaryOfResult { if (mixed === null) { return IsDictionaryOfErrorCode.IS_NULL; } if (mixed === undefined) { return IsDictionaryOfErrorCode.IS_UNDEFINED; } for (let k in mixed) { if (mixed.hasOwnProperty(k)) { const item = mixed[k]; const result = elementPredicate(item); if (typeof result == "boolean") { if (!result) { return { index : k, error : IsDictionaryOfErrorCode.ELEMENT_FAILED_PREDICATE }; } } else { if (result !== undefined) { return { index : k, error : result }; } } } } return undefined; }