13 ms·
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 w
by paxys 3y ago
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 and mire of a world where we are doomed to endlessly crawl on our bellies through the alleys of Github seeking the one true npm. We will forever wonder in the dark, until we have a type reflection model.
What even are they getting at?
As for their actual ask (runtime type safety), it is probably not going to happen because the TypeScript project has drawn the line at not being an alternate or additive runtime for JavaScript. Their job is simply to compile down to JS code and exit the picture. Whatever happens after that (in V8 or elsewhere) is your own business.
> TypeScript Needs to Emit Runtime Type Information
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.
- cjdell 3y agoTypeScript does emit runtime type information through enums though.
- tinideiznaimnou 3y ago[flagged]
- RyanCavanaugh 3y agoI'm here and I have no idea what you're talking about with ESM imports. Please log a bug.
- tinideiznaimnou 3y ago[flagged]
- seattle_spring 3y agoMichael O’Church? Is that you?
- tinideiznaimnou 3y agoLemme check
- tinideiznaimnou 3y agohttps://github.com/microsoft/TypeScript/issues/54163#issuecomment-1538748037 https://github.com/microsoft/TypeScript/issues/54163#issueco... >Hundreds of Github comments have been rehashed on this issue over and over again. Import paths are not modified during compilation, and we're not going to modify them during compilation, and it isn't because we haven't thought about it before and just need to spend another hundred comments arguing about it. >The general tenor of discussion here is not great and I don't think this is likely to lead to any further productive discussion, so I'm just going to lock this. I'm talking about this thing, apologies if I haven't referred to it precisely enough haha. So is it still "no idea", or is it actually "let's pretend we don't hear him, maybe he'll go away" (because you're understandably fed up with answering the same question many times)? --- Listen, how about I just tell you why I'm asking you all those weird little questions. I still want to hear how you feel about being called a "god among mortals" for writing JavaScript for Microsoft, but I mean c'mon. You're a busy man, you just wondered "what's the ploy here" for a sec and decided it's safest not to bother answering them, right? I understand that you, like any software engineer, like to work on impactful and meaningful things, and consequently take a measure of pride in your work; so could I, for a certain project, had I not allowed myself to be pressured into building it in TS - by people with no skin in the game, only the current majority consensus on their side ("all JS bad, but TS least bad"). Let's summarize the original post: - Someone humiliates themselves, literally begging to be heard out. - They bring forth a huge list of things that, beyond reasonable doubt, prove that their concern is valid. - They are pointedly ignored, or rebuffed with some form of "I don't know what you are talking about" or "seems like a you problem". Seeing this dynamic begin to play out in a feature request (out-of-left-field as the whole thing may be), well, that struck a fucking nerve, let me tell you. This has been my exact experience with TS (and no other programming language), when trying to address matters including, but not limited to: - Whether to use TS at all (which I shouldn't have conceded to in the first place.) - Whether TS can be adopted gradually (in my case it took multiple rewrites.) - Whether I'm just imagining that I'm not gaining much by using TS (I'm not.) - Whether I'm just imagining that I'm experiencing drawbacks from using TS (I did.) - Whether TS is "just JS with types" (which is about as true as C++ being "just C with classes".) - Whether the help implicitly offered, conditional on me using TS because "that's what everyone uses now", will ever materialize (which it didn't.) - Whether I was using TS of my own free will (which is only true insofar as I rose to the challenge of accomodating others' supposed "discomfort" with good ol' JS, at my own expense.) Every time I raised any of those questions, I was faced with gaslighting. And yes, in the end I did fall as low as begging my teammates for their help - after all, didn't I just rewrite working JS into nearly-working TS so that it would be more accessible to others? Unrelated subsequent experiences taught me that the (former) coworkers in question might have been just as lost with TS, and even more lost with the JS ecosystem in general (whereas I feel mostly at home with it, coming from Python), but they were reluctant to take accountability and hence admit vulnerability. (Guess adding type annotations doesn't necessarily make code easier to comprehend or maintain, whether you come from a real dynamic language or a real static language, huh.) Well, whatever, that one's on them. And now it's on me to speak out against such insanity as enabled by your product. When I see someone publicly putting themselves through the same situation (or "pretending" to - what's the difference when everyone is on so many layers of irony that it's not even funny anymore?), and it's not Clojure or Rust or Zig or Nim or Julia -- or JavaScript -- but fucking TypeScript once again; and it's not even their coworkers that they're addressing in this manner, ridiculous in its sheer desperation, but they're talking to the fucking upstream, then I'm not going to be a "good sport"; for me, this rapidly turns from "some stupid thing someone wrote on the Internet" into a matter of professional conscience. Why did OP have to communicate in this acutely self-deprecatory manner? Is it perhaps because of a systemic issue in how the TS project handles feedback? Having personally experienced the exact same dynamic when discussing TypeScript, it seems such toxic communication has "trickled down" from upstream to us "cowering meek masses", i.e. the developers of Web-based software, i.e. the people who really should know better, because so much of what we build is intended to be directly consumed by other human beings. For me, this is because TypeScript is not honest open source software. Say it for all to hear: am I wrong that the direction of TypeScript's development is determined by Microsoft's interests first, and the interests of the community a distant second? And is it not misleading and abusive in the slightest to have people learn "Microsoft-flavored JavaScript" instead of the real JavaScript that their browsers can execute, and pretend it's optional when practice shows it's anything but? The stock phrase "incredibly privileged" makes me sick, but in this case you, Ryan, may truly not believe how privileged your position is in comparison to downstream developers around the entire globe. You're not the end-of-line code-monkey just trying to give the non-technicals some buttons they could click; you and your team are literally imposing your wills on a pre-existing community of fellow programmers, leveraging the unlikely synergy of Microsoft's marketing machine and the open source community's network effects. On Monday, you will keep moving TypeScript onward; freely benefitting from the community's input on how to do what you're already doing, only better - more efficiently, more correctly. And just as freely ignoring the community's input on whether you're doing the right thing in the first place. Meanwhile I'll still be recovering from the way your technology ended up impacting my life, no exaggeration, let's not even go there. If even 1% of TS users have been through a similar wringer as myself, that makes for how many people hobbled by your work? Calculate, and consider. All the best.
- johnfn 3y agoThe first 7 words of the document are "TypeScript Needs to Emit Runtime Type Information". Was that not clear? I guess I'm a bit confused by your confusion, unless you don't know what runtime type information is, in which case I suppose it's more understandable - but in that case I suppose you're not really the target audience.
- hn_throwaway_99 3y ago> 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 objects, as opposed to, for example, string literal union types. That is, "enum Foo { Bar = 'BAR', Baz = 'BAZ }" outputs information that can be queried at runtime, while "type Foo = 'BAR' | 'BAZ'" does not. 2. TS already supports runtime type guards functions, https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates https://www.typescriptlang.org/docs/handbook/2/narrowing.htm.... It wouldn't be hard to generate those functions automatically using information already in the type system.
- crooked-v 3y agoTypeScript 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.
- tough 3y agoBut I do like TS Enums, don't take them away
- hn_throwaway_99 3y agoAgreed, 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.
- dclowd9901 3y agoYeah, agreed, this ain’t it. I do think theres room in the world for typescript to be able to emit runtime code from its types, but I don’t think it should be in the business of writing validation libraries. Closest they should get to this work is writing an interface to export types types into runtime code that another lib can pick up and write validators with.
- flohofwoe 3y agoThe Typescript compiler could probably emit additional JS files with reflection info just as it currently emits .map and .d.ts files (basically a 'type database' that exposes the information that's already in the .d.ts file as JS module, and which can be queried at runtime by type- and property-names (e.g. stuff like "what was the original Typescript type for this Javascript property"). The only missing link is then that Javascript objects don't know their original Typescript type, that would need to be magically injected by the TS compiler as a custom property. How useful that would be in practice, no idea... I can pretty much only see downsides (mainly increasing bloat). Also the same thing can probably be implemented in 'user space' by a tool which parses .d.ts files and code-generates such type-database modules from that information.
- deleted 3y ago[deleted]
- SergeAx 3y ago> This is not possible at all because TypeScript is a compiler I don't understand. Go is a compiler too, but here's https://pkg.go.dev/reflect https://pkg.go.dev/reflect
- spatialaustin 3y agoAt the top of the README they link to "The 7 year old Github issue" which describes the problem in they way you're asking for. https://github.com/microsoft/TypeScript/issues/3628 https://github.com/microsoft/TypeScript/issues/3628
- cornstalks 3y ago> > TypeScript Needs to Emit Runtime Type Information > This is not possible at all because TypeScript is a compiler. Sorry, can you clarify? Many compilers exist in other languages that support RTTI, so it's not clear to me what you mean by this.
- paxys 3y agoThere are plenty of projects that do the same for TypeScript (e.g. https://github.com/typescript-rtti/typescript-rtti https://github.com/typescript-rtti/typescript-rtti) and plenty that support some kind of runtime reflection, and overall emitting TS type information at compile time in some readable format is a pretty trivial problem to solve. The complicated part is on the other side – what do you do with this information? How do you get JavaScript engines to understand it?
- wvenable 3y agoYou don't get JavaScript engines to understand it. You use a library.
- paxys 3y agoSo then...exactly how it is done today? TypeScript provides an API to get type info at compile time. Libraries write plugins to consume this type info and use it for runtime and custom validation. What changes in this new world?
- code_biologist 3y ago> So then...exactly how it is done today? TypeScript provides an API to get type info at compile time. That's exactly it, it doesn't. That project you linked (typescript-rtti) mentions in the README that you must install and use it via ttypescript, a typescript wrapper that patches the compilation process to expose compilation details to plugins. Except that plugin API is unofficial and changes break the ecosystem built on it. For example, ttypescript doesn't work for TS 5 and the dev doesn't want to spend time on it. ts-patch has stepped in to provide a source transformer API for TS 5. There's no way I can recommend libraries like typescript-rtti in commercial projects until something changes. I hope the ts-patch folks don't burn out.
- goodpaul6 3y ago> This is not possible at all because TypeScript is a compiler Not sure if this supports your point. Lots of compilers emit runtime type information, e.g. Golang, Java.
- jakelazaroff 3y agoThose languages all have runtimes that can consume the type information. TypeScript does not.
- tylerhou 3y agoNit: 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 would be to use /compile time/ reflection to generate code (TS code hypothetical, because I write C++ nowadays): function validate<T>(obj: Any): T | null { switch constexpr (T) { case String: return typeof obj == "string" ? obj : null; case Array<U> if (!Array.isArray(obj)) { return null; } for (const u of obj) { if (validate<U>(u) == null) { return null; } } return obj; // ... more base cases } for (const prop: (keyof T) of Reflect<T>.Properties()) { if (validate<T[prop]>(obj[prop]) == null) { return null; } } return obj; } interface Date { year: String; month: String; day: String; } It would be great if this could generate /JavaScript/ code: function validate__String__(obj) { return typeof obj == "string" ? obj : null; } function validate__Array$Date$__(obj) { if (!Array.isArray(obj)) { return null; } for (const u of obj) { if (validate__Date__(u) == null) { return null; } } return obj; } function validate__Date__(obj) { for (const prop of ["year", "month", "day"])) { if (validate__String__(obj[prop]) == null) { return null; } } return obj; } Unfortunately this is not possible (AFAIK) in TypeScript currently, and will not be possible with TypeScript's current philosophy. (The above example is a hypothetical TypeScript compiler that might support "templated" generic functions; with just RTTI TypeScript could accomplish the same thing in a non-templated function by passing in `T` as a function parameter at runtime and doing runtime comparisons on `T`.)
- deleted 3y ago[deleted]
- golergka 3y agoI just declare these structures with io-ts, and get validation, serialization, deserialization code and types for free.
- deleted 3y ago[deleted]
- jxf 3y ago> This is exactly why simple, concise writing is essential, and their wall of text is...not it. I only skimmed this but felt it was clear. They're asking for TypeScript, as part of the type erasure, to emit the type information it has discovered about the types in a side channel to the emitted JavaScript. Think of, say, PDB files as an analogy. > They are really asking for a net new product which has very little to do with the TypeScript that exists today. This is information TypeScript already has today but which it discards. It wouldn't take a "net new product".
- RyanCavanaugh 3y agoYou can use the TypeScript API to generate this information at whichever level of detail you want. The level of detail TS has about types during the checking phase is much higher than you would want in practice for 99% of projects (e.g. 1 + 2 + 3 has 6 different types associated with it). The level of detail TS has about types during the checking phase is potentially lower than you would want in practice for a lot of projects (which is critical since that makes the whole feature useless if that happens). For example, the list of properties of a particular generic instantiation is lazily computed, but it's possible your program never pulls on the list so it never exists in the first place, yet is something your type-based tool might want to know.
- jcparkyn 3y agoThey're not asking for reflection on _all_ possible types (which would have the problem you mentioned), just ones explicitly requested at compile time via a function call.
- deleted 3y ago[deleted]
- jauntywundrkind 3y agoCounter to this post, as soon as I read the title I knew what this was, & I knew it was speaking exactly to something we've wanted for a long time. This is asking for more official & better supported https://github.com/rbuckton/reflect-metadata https://github.com/rbuckton/reflect-metadata . TypeScript is a compiler. It has a lot of type information during compilation. We could write that type information out into a file. Instead what we do is throw that information out when the compile ends. Taking all that typing information & throwing it away at the end of compile time is a bad dumb & silly limitation. Especially for a language like JavaScript, which historically could be semi-proud it had such a strong Everything Is An Object philosophy running through it (such as the malleable prototype-based inheritance system); so much type information should be on that Class object. Reflect-metadata for example defined new methods on Reflect to store this metadata. I could not be more delighted to see the pennon of this website go up. We needed a rallying point for this. We needed a rallying point for keeping class data around. A rallying point for enriching the runtime with good actionable data is a good rallying point. It's not what's afoot here, but I think you're a bit off-base about the impossibility of adding even some type-safety. We might not be able to get exact TS type safety. But we can definitely build some safety in. Owing to the malleable prototype-based type system in JS, we can add getters/setters to objects to do a lot of type checking. This doesn't even begin to explore the possibility of what we might do with es2015's proxies, which could allow even more interesting checks to be layered in. I also wish JS had an official AST (and renderer), so had more official options for code-rewriting that might let us weave in type checks. What we can do as programmers is limited by what we have at our disposal. Not throwing out all the typing information, keeping it around at runtime, opens a lot of interesting doors.
- yxre 3y agoWhy not keep the package separate? I also thought of reflect-metadata separately, and it doesn't hurt to allow users to install plugins to augment core compiler behavior. Some people have very tight constraints for payload size, and types could blow up payloads
- jauntywundrkind 3y ago