4 ms·
What I'd really like is a tsc that creates code to check types at runtime, like for API boundaries and parsing unknown input. Kind of like a built-in Zod. Maybe
by nilsbunger 4y ago
What I'd really like is a tsc that creates code to check types at runtime, like for API boundaries and parsing unknown input. Kind of like a built-in Zod. Maybe it's just an automatic type guard anywhere you have an "as SomeType" or an ignore directive.
- brundolf 4y agoMy TypeScript-like language (still a work in progress) does that :) https://www.brandons.me/blog/bagel-bites-refinement https://www.brandons.me/blog/bagel-bites-refinement
- hutzlibu 4y agoMine, too. (also work in progress, but not published) I wonder how many of us there are ...
- eagsalazar2 4y agoThis x1000! Type guards are a joke and real runtime type checking could be so easy if they prioritized it. Yes you can use zod but then you have to define your types through zod which isn't as neat. I actually googled for this just yesterday!
- jitl 4y agoI’m working on a code generator that converts Typescript types to other schema descriptions. One of the conversions is TS -> TypeBox/JSONSchema: https://github.com/justjake/ts-simple-type/blob/main/src/compile-to/json-schema.ts https://github.com/justjake/ts-simple-type/blob/main/src/com...
- phpnode 4y agoMarc J Schmidt has been working on this for typescript at https://deepkit.io/ https://deepkit.io/ and it looks very impressive, and ambitious. I made something similar for Flow a long time ago: https://gajus.github.io/flow-runtime/#/try https://gajus.github.io/flow-runtime/#/try
- underwater 4y agoThanks for making flow-runtime. I used it on a previous project and it was so helpful.
- wk_end 4y agoYou can use io-ts [0] to define your types, and it'll generate functions to typecheck for you. Syntactically it's a bit gnarly and the documentation isn't great; a first-party solution would definitely be nicer. But it works, and it's amazing that it works. [0] https://github.com/gcanti/io-ts https://github.com/gcanti/io-ts
- mhermher 4y agoIt's a neat library, but you end up defining your types in an external (outside of TS) syntax, and you lose a lot of language-server features. Also, last I checked it could not handle generics. But it's been a while.
- wk_end 4y agoIt's not external to TS. You write your types by passing object literals to the functions that generate the validators; TypeScript then infers shockingly precise types, which can be extracted using TypeScript's type manipulation utilities. It does support generics [0]. [0] https://github.com/gcanti/io-ts/blob/master/index.md#generic-types https://github.com/gcanti/io-ts/blob/master/index.md#generic...
- mhermher 4y agoSorry, I meant specifically the "syntax" of your type is not TS. It's those object literal constructions you mention.
- eyelidlessness 4y agoYou don’t lose any language server features, you just access them slightly differently. Each function in io-ts, zod, and other TS libraries like them) are type guards, and have companion utility types to extract the static types from their runtime definitions (with `typeof`). I’m certain that io-ts handles generics (I’ve used it to do so), albeit again slightly differently, in that you need to define them as generic type guards. I think the clamor for runtime type checking in TS is partly because type guards and their benefits could be better explained at the language level, and partly that libraries implementing them effectively are mostly aimed at users who already understand those benefits. You really don’t want pervasive runtime type checking, except at API boundaries where data is untyped (i.e. the appropriate type is `unknown`). Type guards are exactly designed for that use case. For other cases where the types are well known, runtime type checking is needlessly expensive and redundant.
- afavour 4y agoAt some point I used a TS->JSONSchema generator and that worked out pretty great. Obviously it would be greater to be built into the language but I think that's always going to be out of scope of what TS aims to do.
- dtech 4y agoYep, it works pretty well if you combine it with a JSON schema validator and call the validator in a isX(data: unknown): data is X type guard.
- mhermher 4y agoI really wanted this in my earlier usage of typescript as well. But the solution really is to assume `: unknown` at API boundaries and run the values through assertion functions: `isSomeType(x: unknown): asserts x is SomeType`. After using this sort of pattern, I don't think I would want automatic runtime checks anymore, because creating your own explicitly and calling it explicitly works out not so bad.
- madeofpalk 4y agoYou can't actually narrow `unknown` down to a structure yet, as you have no way to test whether `property in unknown` or not. Well, until this is released! https://github.com/microsoft/TypeScript/pull/50666 https://github.com/microsoft/TypeScript/pull/50666
- mhermher 4y agoyeah. You have to assert an object (`Record<string, unknown>`) type first within your asserter (or array or whatever). We ended up having whole stacks of re-usable assertion functions to be used at these boundaries (re-usable on server side too if you're using node!)
- davidzweig 4y agoWe do it like this: https://imgur.com/a/AdeRncw https://imgur.com/a/AdeRncw Just a simple function (ensureType) that checks primitive types (angle brackets for min length). You reconstruct the object using ensureType, writing it back into itself. EnsureType returns what is passed in, with the corresponding type. Has worked well.
- psnehanshu 4y agoCheckout tRPC https://trpc.io/ https://trpc.io/