4 ms·
I'm disappointed that it still doesn't incorporate a tool for one common case, runtime type checking. Node.js REST servers receive a lot of JSON objects and it
by jfengel 6y ago
I'm disappointed that it still doesn't incorporate a tool for one common case, runtime type checking. Node.js REST servers receive a lot of JSON objects and it would be nice to automatically check that they actually follow type.
There are several aftermarket add-ons (io-ts, runtypes, class-validator) but they all seem to impose extra steps or rewriting code -- and different projects will use different ones. It seems like it should be something they can build into the language. I'd rather have one good standard than a dozen alternatives. (Can you guess I come from a Java background?)
- jakelazaroff 6y agoThat is outside of the scope of TypeScript, which is to be a statically typed superset of JavaScript with no runtime cost. It's easy enough to write a runtime type checking library; I wrote a tiny one that I use in basically all my projects: https://npmjs.com/package/narrows https://npmjs.com/package/narrows
- wonderlg 6y agoWith your library one has to write types twice. I understand that it’s not TypeScript’s job at the moment, but it’d be great to enable runtime checks with the flick of a switch without typing extra code. function upper(s: string) { return s.toUpperCase(); } This function could have a nice implicit TypeError instead of “undefined is not a function”
- jakelazaroff 6y agoI’m not sure what you mean. My library has a `TypeOf` type that lets you extract static type information from validation functions. And calling `toUpperCase` on undefined already results in a TypeError.
- joepie91_ 6y ago> That is outside of the scope of TypeScript, which is to be a statically typed superset of JavaScript with no runtime cost. Yes, that is the thing being criticized here.
- playpause 6y agoI agree, it should be built in. But have you tried Zod? It’s amazing.
- throw_m239339 6y ago> I agree, it should be built in. But have you tried Zod? It’s amazing. Not it shouldn't, since it would affect runtime performances.
- moltar 6y agoOr other alternatives https://github.com/moltar/typescript-runtime-type-benchmarks https://github.com/moltar/typescript-runtime-type-benchmarks
- throw_m239339 6y ago> I'm disappointed that it still doesn't incorporate a tool for one common case, runtime type checking. Node.js REST servers receive a lot of JSON objects and it would be nice to automatically check that they actually follow type. You are using the wrong tool for the job then. There are json-schema libraries made for that purpose. It has absolutely nothing to do with Typescript which is just basically a static type analyser, not a language with a runtime, since it does absolutely nothing at runtime.
- xsmasher 6y agojson-schema is hideous compared to TS type definitions, though.
- throw_m239339 6y agojson-schema works at runtime, that's what you want to validate your serialized messages. None of that stuff is related to typescript. Typescript is a static code analyser, nothing more. It doesn't do anything at runtime.
- xsmasher 6y agoI see the utility of json-schema, I just wish the schemas were beautiful and succinct like TS type definitions are. I know one is runtime and one is compile-time, but they both allow you to specify the "shape" of a particular JSON object and verify an example against it.