4 ms·
Hey all, TypeScript PM here. I understand the desire here. Runtime type checking is often necessary for data validation, and we can see lots of libraries devel
by DanRosenwasser 3y ago
Hey all, TypeScript PM here.
I understand the desire here. Runtime type checking is often necessary for data validation, and we can see lots of libraries developed to help fill the gap here. But I think the fact that there are so many libraries with different design decisions is pretty indicative that this is not a solved problem with an obvious solution. We knew this going into the early design of TypeScript, and it's a principle that's held up very well.
What have been happy to find is that we've grown TypeScript to be powerful enough to communicate precisely what runtime type-checking libraries are actually doing, so that we can derive the types directly. The dual of this is that people have the tools they need to build up runtime type validation logic out of types by using our APIs. That feels like a reasonable level of flexibility.
- scotty79 3y agoMaybe official preprocessor plugins for TypeScript compiler could help? I understand that everybody who needs it can already put their own preprocessor that generates runtime objects from type information before the code is passed to tsc for compilation. But the effort is inconsistent and distributed. If TypeScript officially supported pluggable preprocessor and plugin ecosystem for it some good solutions might get discovered.
- andix 3y agoWhat's next? An official TypeScript UI framework, will it be Vue, React, Next.js or Svelte? An official TypeScript date library?
- scotty79 3y agoWhat's the point of any of those? Generating code based on type annotations is frequently requested feature directly related to the core feature of TypeScript which is type system.
- clansimus 3y agoI'll take an official WYSIWYG editor while we're at it
- pjmlp 3y agoThis is the curse of guest languages, after the initial adoption pain everyone wants idiomatic libraries and pretends the underlying platform doesn't exist. Until they hit a roadblock caused by a leaky abstraction, that proves them otherwise.
- andix 3y agoType script does a very good job not to hide the underlying platform. In it's essence it is just a development time linter and does not interfere with the JavaScript runtime at all (except enums). And I think that's actually the reason why it won the competition against Googles Dart. They even used Microsofts TypeScript for Angular instead of their own language Dart.
- pjmlp 3y agoIndeed, and that is why the Typescript team is against any feature that steps away from that relationship.
- 0xb0565e487 3y agoI'm relatively new to programming and had a question about TypeScript's functionality. Is there any specific reason why TypeScript doesn't allow for the creation of custom and intricate data types? For example, I'm unable to define a number type within a specific range, or a string that adheres to a certain pattern (like a postal code). I'm imagining a language where I could define a custom data type with a regular function. For instance, I could have a method that the compiler would use to verify the validity of what I input, as shown below: function PercentType(value: number) { if (value > 100 || value < 0) throw new Error(); return true; } Is the lack of such a feature in TypeScript (or any language) a deliberate design decision to avoid unnecessary complexity, or due to technical constraints such as performance considerations?
- bern4444 3y agoSome of this is possible in the type system like a range: From stackoverflow: https://stackoverflow.com/questions/39494689/is-it-possible-to-restrict-number-to-a-certain-range/70307091#70307091 https://stackoverflow.com/questions/39494689/is-it-possible-... type Enumerate<N extends number, Acc extends number[] = []> = Acc['length'] extends N ? Acc[number] : Enumerate<N, [...Acc, Acc['length']]> type NumberRange<F extends number, T extends number> = Exclude<Enumerate<T>, Enumerate<F>> type ZeroToOneHundred = NumberRange<0, 100> One limitation is that this has to be bounded on both ends so constructing a type for something like GreaterThanZero is not possible. Similarly for zip codes you could create a union of all possible zip codes like this: type USZipCodes = '90210' | ... Often with the idea you have in mind the solution is to implement an object where the constructor does a run time check of the requirements and if the checks pass instantiate the instance and otherwise throw a run time error. In functional programming this is often handled with the Option which can be thought of as an array with exactly 0 or 1 elements always. 0 elements when a constraint is not met and 1 element when all constraints are met. This [0] is a library I wrote for JS/TS that provides an implementation of Options. Many others exist and other languages like Rust and Scala support the Option data structure natively. [0] https://github.com/sbernheim4/excoptional https://github.com/sbernheim4/excoptional
- 0xb0565e487 3y ago
- mirekrusin 3y agoMicrosoft hosted MacroScript as TypeScript plugin or toplevel wrapper would solve this problem. You just need to spark it, community will help maintaining it. It would solve all codegen needs from generating clients to runtime type assertions and many interesting problems in between.
- fouc 3y agothoughts on the other poster asking for type info to be kept around in the Class objects after compilation?