3 ms·
So does TypeScript if you have strict settings. Your input data will be untyped and you will be forced to determine its type before you use it.
by smt88 5y ago
So does TypeScript if you have strict settings. Your input data will be untyped and you will be forced to determine its type before you use it.
- the_gipsy 5y agoTypeScript does nothing like that with any strict settings, because it doesn't provide any JSON parsing alternative, so that is always unsafe. I bet there are runtime checkers that use some kind of reflection, that would be the next best thing. But I also bet that those force you to annotate/decorate everything.
- hermanradtke 5y agoI do not know of a way to make this a compiler error: const user: User = JSON.parse(…);
- dhucerbin 5y agoYou can redefine all definitions from lib.d.ts and similar. In my projects, parse function returns unknown, so I need to parse/validate/decode it in runtime to satisfy types.