r/javascript • u/DanielRosenwasser TypeScript • Mar 09 '23
TypeScript's Migration to Modules
https://devblogs.microsoft.com/typescript/typescripts-migration-to-modules43
u/_xiphiaz Mar 10 '23
Changing the output from 4 spaces to 2 spaces saved considerable disk space? Hate to start a flame war but that seems like a pretty decent justification to use a tab char?
35
Mar 10 '23
Tabs have always been superior, in every way.
So naturally everyone hates them and will use spaces instead
13
u/L0wkey Mar 10 '23
There's no discussion anymore. It's over since it became clear that using tabs is an accessibility feature.
There's absolutely no reason why indent size MUST follow font size. Tabs allows setting whatever indent size they're comfortable with in almost every editor.
3
u/shuckster Mar 10 '23
Tabs are for aligning documents, not code.
In the following, where do you expect the cursor to be when you press tab?
Hi| A B ^ 'cursorAssuming a tab-size of 8 (the classic) should the cursor be at position A or B?
If you guessed "B" because it's 8 spaces beyond the cursor, I'm afraid that's not how tab-stops work:
-------|------|------|------|------| <- tab stops Hi| A B | | | ' two spaces beyond first tab-stop ' first tab-stopNow, todays IDEs and code-editors are of course clever enough to realise that code is code and not a document, so this kind of misunderstanding doesn't have much of a chance to develop in the heads of coders who don't venture outside of their comfy editors.
But if they did they might notice that the archaic doctrines of old cling steadfast to cranky old terminals and graying CLI editors, or even just those of a different stripe. Opening your carefully crafted source in an embedded version of
vifor example (or some obscure online IDE, or even justcat'ing the stuff) will unmask indentation horrors previously concealed by the arcane magic of VSCode and such.But fear not! For there is another character in our midst. One who, upon their summoning, occupies exactly one character-width on the screen for us to style our code however we wish, in any environment, and without surprise or beguilement.
This character is called SPACE, and with careful scrutiny you can find him lurking at the bottom of you keyboard in the shape and proportion of a righteous sword. Wield the sword of indentation clarity for great justice!
10
u/Fidodo Mar 10 '23
Isn't that issue fixed if you simply enforce having tabs at the start of the line only?
0
u/shuckster Mar 10 '23
Probably. I've seen comments in some wild places mind you, and looking at the rest of this thread I can see some peeps do very strange things with function arguments.
Still, spaces are demonstrably more portable, at least if your intention is to have your code format the same way regardless of environment.
6
u/Fidodo Mar 10 '23
I agree it's a bad idea to use tabs anywhere after the line start, but that can be enforced with linters and auto formatting. Is there an issue with portability if that's enforced?
Also, their switch from 4 to 2 spaces was for the compiled output, not for the source code, it's more of a question about if it's suitable for stack traces.
1
u/shuckster Mar 10 '23
Honestly, the whole tabs vs. spaces argument just doesn't matter if you only ever work in modern IDEs. They're clever enough for devs to not even notice.
The "portability" I'm talking about has nothing to do with that though: Just speaking for myself, I have scripts running on little embedded devices running Busybox, as well as Raspberry Pis, and just regular old VPSes hosting a web-site or two.
Discrepancies with rendering tabs show up if I edit my scripts from an ssh session. But actually, even this wouldn't bother me if I'd just configure vi, nano, pico, or whatever.
The real problem is they're embedded devices and I like to tinker with them. Or it's a web-site and I sometimes change host. That means the configs have to be restored if I want the same tab settings across environments.
Again, this isn't an insurmountable problem. I could bake the configs into the firmware or middleware builds, or in the Docker containers or whatever. But then, if I just format everything using spaces I don't even have to think about the problem. I get consistency for zero cost.
Different horses for different courses, though.
2
u/2this4u Mar 10 '23
Not to start this up but sometimes you're aligning to something that's not exactly one tab wide say arguments on new lines under a method. So now you have a mix of tab and space, so why not just use spaces? The IDE can handle is so having a mix of whitespace chars seems unnecessarily complicated.
23
u/nschubach Mar 10 '23
If you are hand aligning anything with spaces you are doing it wrong. I can't think of a single thing that would need this unless you were doing the horrible method of "first param on the same line, and the rest line up with that" nonsense:
function foo (bar, baz, buz) {Just do this instead:
function foo ( bar, baz, buz ) {And you can use a consistent indent.
3
u/ragnese Mar 10 '23
I used to see that first style when I was doing C++ many moons ago. I got used it it and the second style initially really bothered me and seemed "wasteful" of vertical space. I definitely prefer it now.
12
u/xroalx Mar 10 '23
No, you don't.
If you're putting arguments on new lines, then all arguments go on new lines and they're just one more tab further than the function call is.
never(like, this); always( like, this, );4
u/Fidodo Mar 10 '23
They didn't switch from 4 to 2 spaces in their source, they did it for their output, so they could further reduce the memory size by switching to tabs for output so the IDE doesn't actually factor in.
17
u/musical_bear Mar 10 '23
Great read. The part about the overhead from “let” and “const” I found particularly interesting and surprising. Very unique problems in compiler world.
15
u/Thrimbor Mar 10 '23
Can we get a proper language specification?
41
5
u/otherreddituser2017 Mar 10 '23
What a fantastic article, I learned a ton about how TypeScript works and is built. Great job, and keep up the good work!
1
u/jayerp Mar 10 '23
I yearn for the day for TS to get a good Reflection library like .NET.
I can’t imagine making anything robust without the help of Reflection in .NET.
11
u/Reashu Mar 10 '23
Can you elaborate on what you want to do with it that is impossible/cumbersome currently?
3
u/Pelopida92 Mar 10 '23
Whats the usecase ?
0
u/jayerp Mar 10 '23
But alas, it seems like it can be done in TypeScript already so hooray! I want to be able to use something like Reflection to get/set instantiated classes. Let’s me do some pretty awesome things.
5
u/Pelopida92 Mar 10 '23 edited Mar 10 '23
Yeahhh, i don't know about that buddy. Reading your response it seems like you are trying to fit your Java mental models into Typescript. In my experience with Typescript this is not a good idea. When you are using Typescript, you still want to embrace JavaScript mental models to maximize productivity and result. Fighting the language is not gonna give you the results that you are expecting.
1
u/jayerp Mar 10 '23
What’s wrong with using a property lambda to retrieve the property name? I can than use that result as a string to pass to something like MUI data grid to render fields.
2
u/RonSijm Mar 10 '23
What's wrong with the idea, is that it's not really possible... C# isn't just using
Func<T>for this, butExpression<Func<T>>- which represents (the expression tree of) an uncompiled function.So by definition - Typescript is transpiled into plain javacript. There's no JIT transpiling Typescript at runtime.. So you cannot have an uncompiled Typescript expression in transpiled javacript
1
1
u/shuckster Mar 10 '23
Let’s me do some pretty awesome things.
Like what?
1
u/jayerp Mar 10 '23
Come over to C# OOP Funtime land and find out!
1
u/shuckster Mar 10 '23
I must admit, I was hoping for something a little more specific. :)
1
u/jayerp Mar 10 '23
Oh you know, just writing more declarative code by being able to use lands expressions to pull a property name as a string. Which can then be used with things like MUI DataGrid in GridColDef. Also can use it for making validators. Also can use ur for writing serializers, etc. just stuff that makes my life easier.
1
u/shuckster Mar 11 '23
I think JavaScript can already pull property-names as strings:
let objet = { dart: 'a nice bit of' }; let accessor = 'dart'; let whatIsIt = objet[accessor];Or you can use a lens:
let dartLens = lens('dart') let whatIsIt = dartLens.get(objet);Here's what
lenslooks like (just the getter demo'd, no setter):let lens = path => ({ get: obj => path.split('.').reduce( (acc, key) => (acc ? acc[key] : undefined), obj ), })Is that what you mean? Have I understood the use-case correctly?
1
u/jayerp Mar 11 '23
I just want to write a property lambda to get the property name of an object as a string which I can then use the native ways to get or set them.
2
u/shuckster Mar 11 '23
I'm trying to imagine how that's supposed to work: So you don't know the name of the property ahead of time, yet you're trying to access it? What are you actually trying to do?
I only ask because it seems you're asking how to recreate a certain kind of technology (one you're familiar with) rather than asking how to solve a certain kind of problem (in a language you're less familiar with, maybe?)
→ More replies (0)5
170
u/zombarista Mar 10 '23
i regard the ts and the vs-code teams as god-tier developers because of stuff like this; a legendary refactor with virtually nothing broken is something to be very proud of. Great job /u/DanielRosenwasser et. al. Also, a magnificent piece of technical writing. Engaging, good explanations, lots of code samples (which are like pictures for devs), so it's just a really fun article to read.
Thanks for everything you do to make JS better.