josdejong/typed-function

Generated TypeError messages show offending argument _value_, and perhaps better type identification

Open

#127 aberto em 10 de mar. de 2022

Ver no GitHub
 (1 comment) (0 reactions) (0 assignees)JavaScript (22 forks)github user discovery
enhancementhelp wanted

Métricas do repositório

Stars
 (78 stars)
Métricas de merge de PR
 (Métricas PR pendentes)

Description

Currently a typical type mismatch error looks like

TypeError: Unexpected type of argument.
Expected: number or boolean, actual: string, index: 1.

Would it be helpful to show the value of the argument at index 1 in the argument list as well? At least to the extent that something like

`${args[1]}`

would render it? I ask in part because the message is currently showing the "actual type" of the input argument, which can potentially be a little misleading as typed-function certainly allows overlapping types (i.e., there can be JavaScript entities for which more than one of the type test functions would return true, and this aspect is definitely used e.g. in mathjs). So perhaps as an additional enhancement it would be best for the "actual" type to show a union of all of the types whose tests return true on the actual argument? It's probably OK to take a little more processing time at this point since we're about to throw anyway... This latter bit would be an easy change to findTypeName, and looking at that led to finding a bug which I will file separately.

Guia do colaborador