joplin/packages/tools/fuzzer/utils/getProperty.ts

16 lines
413 B
TypeScript

const getProperty = (object: unknown, propertyName: string) => {
if (typeof object !== 'object' || object === null) {
throw new Error(`Cannot access property ${JSON.stringify(propertyName)} on non-object`);
}
if (!(propertyName in object)) {
throw new Error(`No such property ${JSON.stringify(propertyName)} in object`);
}
return object[propertyName as keyof object];
};
export default getProperty;