2 ms·
Was using JS for about 6 yrs now and decided will start using TS for a new Node.js project. Admitted it has its warts ... hit my first bottleneck rightaway when
by hakcermani 6y ago
Was using JS for about 6 yrs now and decided will start using TS for a new Node.js project. Admitted it has its warts ... hit my first bottleneck rightaway when parsing the request query param. TS Express bindings declare it as string | string[] | ParsedQs | ParsedQs[]. Ok extract it 'as' string and move on. But I find as I am using it more and more it is getting better (or I am getting more disciplined!) .. maybe I am reading this wrong re the long compile time reported, could the code not be broken into modules so the compile is more isolated ??
- emptysea 6y agojust 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 ..