Live data from Hacker News

TypeScript Without Transpilation (2021)

incrementalelm.com

31–40 of 46 posts

Re: TypeScript Without Transpilation (2021)

#31
post #22

Earlier quoted context omitted.

Not TypeScript the language, but TypeScript the typechecker program. “Typescript without transpilation” is still perfectly accurate.

Vouched. This is the point of the thing - the typechecking benefits come from running `tsc` on your code (or VSCode will do it for you in the background and show you linter errors if you have typescript installed somewhere it knows about). Incidentally I've been doing my JS projects this way for 2-3 years and I really dig it.

I started one side project like this recently and it's been great so far.

Re: TypeScript Without Transpilation (2021)

#33
post #31
post #22

Earlier quoted context omitted.

Vouched. This is the point of the thing - the typechecking benefits come from running `tsc` on your code (or VSCode will do it for you in the background and show you linter errors if you have typescript installed somewhere it knows about). Incidentally I've been doing my JS projects this way for 2-3 years and I really dig it.

I started one side project like this recently and it's been great so far.

There's a few clunky bits but I hardly ever run into them.

Actually since this conversation is going, here's something I just learned this week that somebody might find helpful. I used to consider enums one of the clunky bits, but I just ran across the `keyof` operator:

    var modeNames = { FOO: 1, BAR: 2 }

    /** @param {keyof modeNames} mode */
    export function doThing(mode) {
        // the jsdoc type above is equivalent to {'FOO'|'BAR'}
    }
Probably old hat to TS people, but since I came at it from the other direction (I'd been using JSDoc for traditional doc-creation reasons before I realized VSCode could use it for linting and type hints) it was news to me.

Re: TypeScript Without Transpilation (2021)

#34
post #3

Sorry for being that guy, but this should probably be called "typechecking without transpilation". What ends up being written is not TypeScript syntax (which does require transpilation to run). JSDoc is awesome for incremental adoption though, I've migrated a few codebases this way without too much pain.

Not TypeScript the language, but TypeScript the typechecker program. “Typescript without transpilation” is still perfectly accurate.

That's my one grief with tsc: it does too many different things.

- Type checking

- Transpiling to multiple formats (CJS/ESM, JSX and the like)

- Generating declaration files (.d.ts)

While there are modern alternatives for transpiling, much faster than tsc (esbuild, swc), typechecking and declaration generation are still very slow operations.

Re: TypeScript Without Transpilation (2021)

#35

Does anyone know if I have a TypeScript project, is there an automated process to turn it into this format this article describes? Eject for TS, I guess.

You can probably do this with the typescript compiler API https://github.com/microsoft/TypeScript/wiki/Using-the-Compi...

Re: TypeScript Without Transpilation (2021)

#36
post #18

Earlier quoted context omitted.

Could that improve execution performance?

Runtime type checks usually decrease the performance.

Right, runtime type checks do...and JavaScript has to do them pervasively. Hence OP's question - could these type annotations eliminate the need for type checks at real time by making type assertions static (the answer is no, but that was the question).

Re: TypeScript Without Transpilation (2021)

#37

JSDoc comments are definitely not the “best of both worlds,” they are a limited and verbose subset of typing. I would only use them for specific reasons, like I’m in a hostile ecosystem that dogmatically doesn’t want any build steps (like WebGL/WebGPU engines).

It’s also really nice to have annotations describing what a function or object does, consuming a library is considerately worse without them, as they often prevent the need for looking up documentation, and they can even link to the right documentation page (although that is very rare).

When I write them I don’t annotate types though, that’s already done by typescript, and editors like vscode even gives you type hints in a js file if it detects d.ts files in the source.

Re: TypeScript Without Transpilation (2021)

#38

Does anyone know if I have a TypeScript project, is there an automated process to turn it into this format this article describes? Eject for TS, I guess.

Typescript is a superset of Js/tsdocs so you’ll probably get a long way but at some point the types will be dumbed down.

Re: TypeScript Without Transpilation (2021)

#39
post #10

I've found a happy medium to be annotating functions etc with JSDoc style comments but keeping interfaces etc. in a .d.ts file. The Typescript compiler is able to import it from a .js file just fine.

Agree, any complex types just get placed in their own `.d.ts` file, and import that into JSDoc comments

  /// Top of file usually
  /**
   * @typedef {import('./typedefs/MyThing')} MyThing
   */
  
  /// Later
  
  /**
   * @param {MyThing} x
   */
  function(x) {
    //
  }

Re: TypeScript Without Transpilation (2021)

#40
This is exactly how I use typescript, as a linter and static type analyzer for javascript. Unfortunately, typescript support for JSdoc is quite basic. So that method has its limit. But I'm not committing any typescript code.

Someone here just suggested to import type definition files with JS doc annotations, and that's actually a good idea.

Post reply on HN