Live data from Hacker News

TypeScript please give us reflection/runtime types

github.com

181–190 of 287 posts

Re: TypeScript please give us reflection/runtime types

#181
This comes up so often, and every time I wonder how much the people who want it have a shared understanding of what it is they want. I realize this is reiterating a point that TS team members have already made, but I’m coming from a different perspective: actually having built higher level tooling on top of one of the solutions mentioned in the post, and having built a fairly mature prototype of another solution from the ground up to address problems none of the existing offerings solve.

My use case isn’t particularly outlandish, I can even imagine it being non-niche if I ever get around to publishing it. But it’s also pretty far afield from any of the existing solutions: platform-agnostic documentation as a core/foundational principle.

My original work implemented JSON Schema documentation on top of io-ts, and added some runtime and type level functionality to support that. But my vision for a hypothetical successor is that

- the documentation standard itself (whether JSON Schema, OpenAPI, or any other underlying format) should be the underlying primitive

- the underlying format should be composable to produce APIs which are just as user friendly (ie users shouldn’t have to muck around with the doc format unless they have a really specific use case, and even then they should still be able to compose the parts they’re not concerned with)

- no additional build time codegen tooling: define a schema once, its type is inferrable from the definition without a compiler extension or IDE config, its documentation is a plain function call executable in any standard runtime to resolve what’s composed

This is all very achievable in TypeScript as-is. But it’s way out of scope from what I imagine any first class thing would or should be. And sure, if such first class thing existed, that wouldn’t make my thing any less achievable. But here’s what I think it would do:

1. Disincentivize usage by users who prefer the first class thing with type syntax as the primary authoring mechanism.

2. Incentivize me to cave on tooling to accommodate that.

3. Piss people off because “we wanted less tooling not more”.

4. Recurse into this same debate.

I’d personally prefer the status quo. Even if that means rehashing the same debate every few months, at least it’s at a recursion depth that’s manageable and doesn’t have countless other sets of unintended consequences.

Re: TypeScript please give us reflection/runtime types

#182
post #43

I had to read their problem statement like 4 times to understand what they are asking for. This is exactly why simple, concise writing is essential, and their wall of text is...not it. > I love you. You do amazing work. You are gods among mortals. You have brought JavaScript from the darkness, and given it the warm light of strong typing. Look upon us, the cowering meek masses, and understand that we live in the muck…

[flagged]

I'm here and I have no idea what you're talking about with ESM imports. Please log a bug.

Re: TypeScript please give us reflection/runtime types

#183

Earlier quoted context omitted.

Preach, brother! If only ECMAScript had a native macro system, TypeScript could be just a library - and I would have zero problems with its existence. You're probably familiar with the following anecdote: >Eich originally joined intending to put Scheme "in the browser",[4] but his Netscape superiors insisted that the language's syntax resemble that of Java. JavaScript became the language that we all love to hate due…

If ECMAScript had macros such that TypeScript could be a library, people would still ask the question: can we attach the type object manipulated by the library to the objects? If ECMAScript had macros, page load times would skyrocket. ECMAScript and/or browsers would have to define a way to distinguish JS-with-macros files and embedded scripts from plain old JS that doesn't require expansion. Any JS code bases with c…

You raise fair points.

>So, back to the same model as TypeScript.

I beg to differ. There's a world of difference between:

1. a standardized feature with an official spec, which is subject to debate among the community, and of which it's viable to have multiple competing implementations; vs

2. a proprietary product that presents itself to be a superset of the core language (but isn't), is peddled by a single multinational, has a single implementation designed by an unaccountable elite in the employ of said multinational, which just so happens to be the original Dumbing Down The Computer Company

>If ECMAScript had macros, page load times would skyrocket.

>ECMAScript and/or browsers would have to define a way to distinguish JS-with-macros files and embedded scripts from plain old JS that doesn't require expansion.

If you introduce them today by way of a polyfill, maybe. I'm not necessarily advocating for introducing native macros to ECMA-262 at the present juncture. But I can't help speculating what would've happened if they had caught on.

If they were introduced at the point in history when people were first realizing transpilers were a thing; or designed into the language from the start; or a pre-existing metaprogramming-aware language was used in browsers, instead of Eich designing in 10 days an entire new language under the constraint that it also has to be an ad for Java - who knows where we would be as a civilization? What do you think?

Re: TypeScript please give us reflection/runtime types

#184

Earlier quoted context omitted.

> This is not possible at all because TypeScript is a compiler. They are really asking for a net new product which has very little to do with the TypeScript that exists today. That's not correct. All you would really need to do is output type information as JS objects and then support reflection libraries that looked up information at runtime. Two examples where this already happens: 1. TS enums are output as JS obje…

TypeScript enums only exist at all because they were included early on before the project really narrowed on its current goal of being "1-to-1 current JS, but with types". If TypeScript were started from scratch right now with the current philosophy, enums would never exist in the first place.

But I do like TS Enums, don't take them away

Re: TypeScript please give us reflection/runtime types

#185

Earlier quoted context omitted.

> This is not possible at all because TypeScript is a compiler. They are really asking for a net new product which has very little to do with the TypeScript that exists today. That's not correct. All you would really need to do is output type information as JS objects and then support reflection libraries that looked up information at runtime. Two examples where this already happens: 1. TS enums are output as JS obje…

TypeScript enums only exist at all because they were included early on before the project really narrowed on its current goal of being "1-to-1 current JS, but with types". If TypeScript were started from scratch right now with the current philosophy, enums would never exist in the first place.

Agreed, but two points:

1. I was responding to the point that seemed to be arguing that just because TS is a compiler (to JS), that it couldn't support runtime type info. That's incorrect, and different from the current philosophy of "TS should really only use type erasure when outputting JS code".

2. After many years I've come to the conclusion that there is huge benefit for an ecosystem to either (a) have a "batteries included" mindset, or (b) have a way to "semi-officially" designate associate libraries as being supported. I think Java really excelled here. For example, the Java Collections library was/is excellent, and for a long time (not sure if it still is, I've been out of the Java ecosystem for some time now) Apache Commons were the go to place for libraries everyone used. Contrast that with the Node/JS ecosystem, where basically "whatever gets the most popular in NPM" becomes a semi-standard, but there are still often 5 competing libraries, and until the module owner decides to delete a widely used library or just stop releasing updates - besides leftpad infamy, there was also an issue where lodash basically went unreleased for a long time despite needs for critical security patches.

When it comes to TS, I understand the guiding principle of "we only implement type erasure", but I wish there at least a way to e.g. set a tsconfig flag if you wanted to allow runtime data, or to have the equivalent of Apache Commons for TS.

Re: TypeScript please give us reflection/runtime types

#186

Earlier quoted context omitted.

Nit: they're not asking for runtime type safety; they're asking for the ability to reflect on types (at compile time, to generate values) so that they can use type information at runtime. This helps ensure runtime type safety because (for example) it would be great to have a generic "validation" function that takes an arbitrary interface and an arbitrary object and validates that object. One way to implement this wou…

Not to be flip, but if it were really all this easy, we would have done it already. There are dozens of questions you can throw at this code: What if the input's a union? What if it's a nested union -- how do you avoid combinatorial explosion? What if the input is a function -- how do you validate its parameter types using runtime information? What if the input is a conditional type? What if you're inside a generic f…

Maybe I'm very mistaken, but to me it seems this code snippet is basically an alternative to writing a ton of "infer"-s and overloads, no? So the same pattern matching could be used in the "switch". Whatever the complier knows can be locally matched, and the combinations have to be already handled by the developer.

Re: TypeScript please give us reflection/runtime types

#188
Since it's not explicitly listed there, I feel I should shout out David Blass and his incredibly cool ArkType project[1]. It's typescript wizardry.

He sometimes (used to?) streams himself working on twitch and it's a really comfy place to hang out.[2]

[1] https://github.com/arktypeio/arktype

[2] https://www.twitch.tv/arktypeio

Re: TypeScript please give us reflection/runtime types

#190

Earlier quoted context omitted.

Nit: they're not asking for runtime type safety; they're asking for the ability to reflect on types (at compile time, to generate values) so that they can use type information at runtime. This helps ensure runtime type safety because (for example) it would be great to have a generic "validation" function that takes an arbitrary interface and an arbitrary object and validates that object. One way to implement this wou…

If that's so much needed I wonder why there isn't a thriving ecosystem of pluggable typescript preprocessors that add whatever values based on types direcly to typescript source before compilation.

There are a lot, and some are mentioned here in the comments.
Post reply on HN