Optional
errorFactoryGeneric: string | ((key) => string | Error)interface Animal {
id: number;
name?: string;
collarChipNumber?: string;
age?: number;
}
const spot: Animal = {id: 36, name: 'Spot', age: 5};
const get = genNakedPropGetters(spot);
const result = {
id: get.orCrash('id'),
name: get.orNull('name'),
collarChipNumber: get.orUndef('collarChipNumber'),
age: get.orZero('age')
};
// result: {id: 36, name: 'Spot', collarChipNumber: undefined, age: 5}
// with custom error
const getWithCustomError = genNakedPropGetters(emptyObj, key => `Custom error: ${key} is missing`);
getWithCustomError.orCrash('name') // crashes with 'Custom error: name is missing'
// or custom error during usage
getWithCustomError.orCrash('name', 'no name') // crashes with 'no name'
Utility function for generating property getters for one specific object. Functionally similar to propOrNullNaked, propOrUndefNaked, and propOrZeroNaked, but it has swapped arguments and only supports naked objects.