5 ms·
Interesting how typescript plays into this - I mean back in the wild old days of plain old JS I would be totally fine with putting a JSON.parse here and there,
by seer 7y ago
Interesting how typescript plays into this - I mean back in the wild old days of plain old JS I would be totally fine with putting a JSON.parse here and there, especially on the hot path.
But now with static types - this would totally wreck static type checking. And you would need to spend additional cycles to validate that the data is actually correct.
Definitely a change request in the PR.
This has to be probably a really big validate perf advantage to warrant the loss of static checks.
- bcoates 7y agoCan JS static typing not evaluate constant expressions to infer types?
- matt_kantor 7y agoTypeScript has great type inference, but there's no way to get it to parse JSON strings at type-checking time.
- WorldMaker 7y agoTypescript gives you type checking if you import from a JSON file. (Node handles JSON imports and webpack will happily build that for you into a JSON.parse in a bundle.)
- eterm 7y agoTo be honest I think it's a big mistake on the part of typescript to not have a JSON.parse<T>.
- matt_kantor 7y agoThat would just obscure the lie. I'd rather see an explicit `as T` cast at the call site to make the "trust me, typechecker, I know what shape this is" claim be in-your-face instead of hidden behind a type parameter. (This reply assumes you're not asking for TypeScript to make a major philosophical shift and start generating runtime code to validate types. If you are, that's a discussion worth having but goes way deeper than `JSON.parse`.)