Last active
July 23, 2022 03:34
-
-
Save felippe-regazio/9e1d95469cd853145d52d6646852f890 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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