Last active
September 1, 2022 02:56
-
-
Save ericelliott/04ef5f90b4268598f73a75151af0684f to your computer and use it in GitHub Desktop.
Naive runtime type checking example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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