Skip to content

Instantly share code, notes, and snippets.

@ericelliott
Last active September 1, 2022 02:56
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericelliott/04ef5f90b4268598f73a75151af0684f to your computer and use it in GitHub Desktop.
Save ericelliott/04ef5f90b4268598f73a75151af0684f to your computer and use it in GitHub Desktop.
Naive runtime type checking example
const fn = (fn, {required = []}) => (params = {}) => {
const missing = required.filter(param => !(param in params));
if (missing.length) {
throw new Error(`${ fn.name }() Missing required parameter(s):
${ missing.join(', ') }`);
}
return fn(params);
};
const createEmployee = fn(
({
name = '',
hireDate = Date.now(),
title = 'Worker Drone'
} = {}) => ({
name, hireDate, title
}),
{
required: ['name']
}
);
console.log(createEmployee({ name: 'foo' })); // works
createEmployee(); // createEmployee() Missing required parameter(s): name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment