Live data from Hacker News

TypeScript is now officially 10 years old

coderoasis.com

171–180 of 207 posts

Re: TypeScript is now officially 10 years old

#171

Earlier quoted context omitted.

> Typescript is for big project and team collaboration. TypeScript is for any kind of project, it just makes your code safer, easier to debug, easier to read / come back to in the future, etc. > Fast! That doesn't mean anything. Especially for a small project, the TypeScript to JavaScript compilation will take milliseconds, there is no speed impact at all.

My project is small but TS takes 4-5 seconds to compile it from scratch on each run. The main speed impact is in developer productivity though. If something ain't working right, now I first gotta fix the types before I can see if I've fixed the actual logic. I imagine if my codebase was more "OOP-y" (i.e. if I replaced every layer of my domain model with 3 layers of dependency injection, turning the whole thing into…

It's always fascinating how different people approach writing code. I've worked with some very talented devs who preferred dynamic languages because they liked what I call a "smash face on keyboard" style of coding. They wanted to be able to make a change, run the code, make a change, run the code, often repeating that dozens of times before they got it right. I, and I think many people who prefer statically typed languages prefer a more methodical style of programming where we spend more time reasoning about the types and how data flows through the app. I might only run the code every five or ten minutes, but often if it builds it's right the first time. If the types are wrong, the code is wrong, and I've no interest in running code that's wrong.

Is one side or the other right or better? I dunno, I'm not qualified to answer.

Re: TypeScript is now officially 10 years old

#172
post #171

Earlier quoted context omitted.

My project is small but TS takes 4-5 seconds to compile it from scratch on each run. The main speed impact is in developer productivity though. If something ain't working right, now I first gotta fix the types before I can see if I've fixed the actual logic. I imagine if my codebase was more "OOP-y" (i.e. if I replaced every layer of my domain model with 3 layers of dependency injection, turning the whole thing into…

It's always fascinating how different people approach writing code. I've worked with some very talented devs who preferred dynamic languages because they liked what I call a "smash face on keyboard" style of coding. They wanted to be able to make a change, run the code, make a change, run the code, often repeating that dozens of times before they got it right. I, and I think many people who prefer statically typed la…

TypeScript doesn't entirely prevent you from doing that, even if you have to stick in a few "any" keywords here and there and ignore whatever linter warnings you might get. I'm happy for individual devs to work like that if that's what works best for them to get their code going - but I'm certainly not happy for a shared codebase for a large complex project with dozens of devs on it to operate the same way.

Re: TypeScript is now officially 10 years old

#173
post #170

Earlier quoted context omitted.

Okay... $ echo "export class Foo { a = 1 }" > test.mjs $ npx ts-node > import('./test.mjs').then(console.log) error TS7016: Could not find a declaration file for module './test.mjs'. '/home/user/Lab/test.mjs' implicitly has an 'any' type. $ npx ts-node -O '{"allowJs":true}' > import('./test.mjs').then(console.log) Promise { } Error [ERR_REQUIRE_ESM]: require() of ES Module /home/user/Lab/test.mjs not supported. $ ech…

ts-node isn't an official part of the TypeScript project. It has notoriously bad module support. Try this: // tsconfig.json { "compilerOptions": { "outDir": "dist", "module": "es2020", "allowJs": true }, "files": ["index.ts"] } // index.ts import('./test.mjs').then(console.log) > npx tsc && node ./dist/index.js [Module: null prototype] { Foo: [Function: Foo] }

I'm not even talking about the module support. (It just seems to have the default support of TypeScript.)

Yes, it imports the module. No, it doesn't do even basic type inference (knowing that Foo is a class and consequently allowing it to be used as a type name) - which it would, if it was a superset of JS. Instead it seems to import everything as "any", which is... a start, I guess?

Re: TypeScript is now officially 10 years old

#174

Earlier quoted context omitted.

> I'm of the functional persuasion, yet I've found that classes are the ony way to write TypeScript that fits on your screen at all. Huh? I’m of the functional persuasion too, and I use classes in TS too, but for strategic reasons (well defined value objects are easier to reason about than duck typed POJOs, and they perform better too). But I’ve never found them more space-dense than the equivalent function-only code…

>you can’t have explicit type defs without explicit defining them somewhere Sure, as long as I have to define them once and exactly once . Not always possible, as in the case of simple, garden-variety keyword arguments. // foo is required, bar has default, baz is optional type POJO = { foo: number, bar: number, baz?: number } function myFn ({ foo, bar = 3, baz }: POJO = {}) { // oh wait... function myFn ({ foo, bar,…

> Not always possible, as in the case of simple, garden-variety keyword arguments.

   // foo is required, bar has default, baz is optional
All of your examples are correctly identified by TS as type errors, because they all have a default argument which will never bind `foo`. True in JS as well as TS. Consider the untyped code, with some access to a required `foo`:

  function myFn ({ foo, bar = 3, baz } = {}) {
    return bar + (baz ?? foo);
  }

  myFn(); // NaN
Your function shouldn’t supply a default argument, because if the foo property is required the object containing it has to be too. It should instead supply defaults to the properties in it. This is closer to what you seem to want:

  function myFn (pojo: POJO) {
    const { foo, bar = 3, baz } = pojo;
    // foo is required, bar has default, baz is optional

    return bar + (baz ?? foo);
  }

  myFn(); // Compile error
  myFn({}); // Compile error
  myFn({ foo: 2 }); // 5
  myFn({ foo: 2, bar: 4 }); // 6
  myFn({ foo: 2, bar: 4, baz: 6 }); // 10
  myFn({ foo: 2, baz: 6 }); // 9
> But... it doesn't even try to infer the type of an untyped destructuring - even if it's a local function used only once!

Yes, it does, if the thing you’re destructuring is typed. It doesn’t infer from usage, and while that sounds nice and some languages have it, it's fairly uncommon.

> oh wait... parameter can't have question mark an initializer

Yeah. It can’t. But if you applied what I suggested, it compiles and has the type you expect without adding undefined:

  type Foo = { defaultBar?: Bar }

  function main (foo: Foo, bar: Bar = foo.defaultBar) {}

  type Main = typeof Main; // function main (foo: Foo, bar?: Bar | undefined) {}
> Probably because it compiles them to a pre-standard, ES5-compatible class implementation based on good ol' `Foo.prototype`. And since they've already handled them one way, they can't become spec-compliant without breaking backwards compatibility.

You’re partly right. I’m on mobile so I can’t dig into the failure but type checker seems to be crashing or getting stuck due to the confusing syntax. If you make it more clear by putting parentheses around the class expression, you still won’t get any compiler errors because it’s constructed with no arguments, and a is implicitly assigned undefined which satisfies any. If you then give the a property a non-any/unknown type you’ll get a compile error because a wasn’t assigned.

It’s weird that even newer compile targets don’t get an assigned a: undefined at runtime, and definitely qualifies as a compiler bug (you should file it! I’ll add what I’ve learned!). It certainly does if you actually assign anything to a during construction.

> Everybody's build tools been compiling that back down to CJS so hard that Node.js 16+ introduced intentional incompatibilities between CJS and ESM modes just to get people to finally switch to the standards-compliant module system.

This is factually false. CJS is fundamentally incompatible with ESM, and has been since day 1. They shipped it incompatible from at least Node 12 because there’s no way to make it compatible. ESM is fundamentally async, and CJS is fundamentally blocking. ESM imports are “live”, CJS are static values at the time you call require. They have fundamentally different module resolution algorithms. All of this has been documented in Node also since at least v12, and has been spec compliant (notwithstanding since fixed bugs) the whole time.

There are definitely valid gripes about how TS has supported ESM though, particularly in terms of file extensions. Thankfully they’re actively working to address that now.

Re: TypeScript is now officially 10 years old

#175

Earlier quoted context omitted.

Typscript: "function area(left: number, top: number, right: number, bottom: number): number {" Javascript: "function area(left, top, right, bottom)" In an editor the typescript function would likely be split into five separate lines. "function area( left: number, top: number, right: number, bottom: number) : number {" None of the functions are complex but the the typescript function with the formatter creates a lot o…

It's not noise, it's informative content. It also means it's not going to explode when fed the wrong types, because that's not possible.

The only way it can not be noise is if you have no idea what your types are. That's a terrible way to code.

If I'm working on a function foo(bar, baz), I know exactly what bar and baz are before looking at a single line of it, I've been tracing the code through to that function, how could I not know what the data types are at that point?

Re: TypeScript is now officially 10 years old

#176

TS on frontend feels like devs making their editor choice (VScode) a hard dependancy

What do you mean? I'm editing TS perfectly fine in Jetbrains IDEs, and some coworkers use emacs and vim without any issues, still getting all the error highlights, etc. Is there an editor that doesn't work with Typescript? Even without type error integration into the editor view, you could still run tsc in your shell of choice to manually check if everything is OK.

> I'm editing TS perfectly fine in Jetbrains IDEs, and some coworkers use emacs and vim without any issues, still getting all the error highlights, etc.

That's why you don't have any issues. The error popups aren't the problem. It's the autocomplete and type popups.

The last few years TS has raised an army of shit devs that code in such a way that it's literally impossible to read the code without your IDE popping up multiple boxes to tell you what is happening. They just search every function name, giving zero thought to where anything lives in the directory structure. They type everything like fucking idiots, with layers upon layers of types across 10+ files, all of which you have to open just to find out you're working with an object that has a couple of strings and a number on it. Of course VSCode will just tell you this when you hover over the variable, if you're using it.

Just wait until you get a project where these muppets have a majority and you'll feel the pain.

Re: TypeScript is now officially 10 years old

#177

Earlier quoted context omitted.

It's not noise, it's informative content. It also means it's not going to explode when fed the wrong types, because that's not possible.

The only way it can not be noise is if you have no idea what your types are. That's a terrible way to code. If I'm working on a function foo(bar, baz), I know exactly what bar and baz are before looking at a single line of it, I've been tracing the code through to that function, how could I not know what the data types are at that point?

It's normal not to be aware of the types when encountering code for the first time, or after a long absence.

Your described experience is the rare exception when working with third party vendors, on long lived projects, or with teams that are not trivially small.

Re: TypeScript is now officially 10 years old

#178

Earlier quoted context omitted.

It's not noise, it's informative content. It also means it's not going to explode when fed the wrong types, because that's not possible.

The only way it can not be noise is if you have no idea what your types are. That's a terrible way to code. If I'm working on a function foo(bar, baz), I know exactly what bar and baz are before looking at a single line of it, I've been tracing the code through to that function, how could I not know what the data types are at that point?

The other people on/joining your team probably don’t know.

Re: TypeScript is now officially 10 years old

#179
post #81

Earlier quoted context omitted.

I personally can’t agree with that. TS catches me writing numerous bugs a day, the structural typing plus static analysis is a superb combination. If your code base is in JS without types, you probably have many bugs, you just don’t know about them. Especially around undefined/null handling. TypeScript also allows me to refactor fearlessly, which substantially improves the quality of my code as I can do mini-rewrites…

> TS catches me writing numerous bugs a day I see this sentiment a lot but I honestly can't think of any non-trivial bugs that TS has caught for me. 99% of the bugs it catches I would see 1 second later when my page hot reloads and crashes.

Runtime errors are the easiest to catch and fix.

With TS you're paying the full price (in verbosity and complexity) but getting 10% of the value. There probably still are projects where it's a good deal, but my bet is they are the minority.

Re: TypeScript is now officially 10 years old

#180

Earlier quoted context omitted.

The only way it can not be noise is if you have no idea what your types are. That's a terrible way to code. If I'm working on a function foo(bar, baz), I know exactly what bar and baz are before looking at a single line of it, I've been tracing the code through to that function, how could I not know what the data types are at that point?

It's normal not to be aware of the types when encountering code for the first time, or after a long absence. Your described experience is the rare exception when working with third party vendors, on long lived projects, or with teams that are not trivially small.

So the benefits are only available once or twice, but the code is going to be verbose forever.

If I were to come up with environment where it is guaranteed to be a net positive it would be a company with lack of boundaries between teams and a lot of churn.

Essentially TS averages out engineers in your team. You're going to be slower, but more predictable. Your 10x engineers will become 5x, but 1x will become 2x.

Post reply on HN