4 ms·
just a heads up, casting `as string` isn't safe since the request param could turn out to be an array. If the request ends up with the query param as an array,
by emptysea 6y ago
just a heads up, casting `as string` isn't safe since the request param could turn out to be an array.
If the request ends up with the query param as an array, there isn't a runtime to validate the cast and now the variable is typed as `string` but is actually `string[]`.
I think a safer approach is to use a validation library or refine the type with an if stmt and returning an error when it isn't a string.
I use the following lint to prevent all casts except `as const`: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/consistent-type-assertions.md https://github.com/typescript-eslint/typescript-eslint/blob/...
- hakcermani 6y agoThanks for the headsup. I did realize that. It was a specific case of pulling a username from the query .. checking out the library ..