Skip to content

Instantly share code, notes, and snippets.

@felippe-regazio
Last active July 23, 2022 03:34
Show Gist options
  • Save felippe-regazio/9e1d95469cd853145d52d6646852f890 to your computer and use it in GitHub Desktop.
Save felippe-regazio/9e1d95469cd853145d52d6646852f890 to your computer and use it in GitHub Desktop.
function typed(t, ...args) {
args.forEach(value => {
const typeMatch = t === 'array' ? Array.isArray(value) : typeof value === t;
if (!typeMatch) {
/**
* Você pode fazer o que quiser aqui. Se quiser parar a runtime,
* vc pode lançar um erro (throw new Error()). No nosso caso apenas
* vamos avisar que tipos estão diferindo mas deixar o programa
* continuar. Usamos console.warn + new Error inves de console trace
* porque esse ultimo vem em formato de mensagem simples.
*/
console.warn(new Error(`Type mismatch for value: ${value}. Expected it to be: "${t}"`));
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment