July 6th, 2024

Speeding up the JavaScript ecosystem – Isolated Declarations

TypeScript's new isolated declaration feature in version 5.5 simplifies packaging, reduces type definition file creation time to less than a second, eliminates manual *.d.ts file creation, and enables easy code sharing without npm packaging complexities. This innovation enhances code-sharing efficiency and streamlines .d.ts file generation by requiring explicit return types for exported functions, revolutionizing the packaging and sharing process for developers.

Read original articleLink Icon
Speeding up the JavaScript ecosystem – Isolated Declarations

TypeScript's new isolated declaration feature in version 5.5 is transforming code sharing by simplifying packaging and reducing the time to create type definition files from minutes to less than a second. This feature eliminates the need to manually create *.d.ts files, making the publishing process faster and more efficient. By shipping TypeScript source code instead of .d.ts files, developers can now easily share code without the complexities of packaging for npm. Isolated declarations streamline the generation of .d.ts files by requiring explicit return types for exported functions, significantly reducing the time it takes to create them. This approach allows for on-the-fly generation of definition files when installing a package, simplifying the process of packaging and sharing code. The system has been successfully implemented in Deno and is compatible with various package managers, making it a game-changer for developers looking to streamline the code-sharing process.

Related

NPM and NodeJS should do more to make ES Modules easy to use

NPM and NodeJS should do more to make ES Modules easy to use

Boris Cherny discusses challenges with ES Modules in NodeJS and NPM, proposing solutions like simplifying file extensions, upgrading libraries, and phasing out CommonJS support in NodeJS to boost adoption rates.

AdonisJS

AdonisJS

AdonisJS is a TypeScript-first web framework for Node.js, emphasizing type-safety, intellisense, and performance. It offers testing support, official packages like Lucid for SQL ORM, Auth for authentication, and a vibrant community.

Show HN: Synapse – TypeScript Toolchain for Cloud Apps

Show HN: Synapse – TypeScript Toolchain for Cloud Apps

Synapse is a full-stack TypeScript toolchain with resource-driven programming, cloud-agnostic libraries, and fine-grained permissions. It includes a TypeScript compiler, fast package manager, and testing framework for local or AWS deployment. Installation instructions vary by OS. Leveraging TypeScript, esbuild, Node.js, Terraform, and AWS SDK. Detailed documentation on GitHub covers Custom Resources, Environments, Packages, and Tests, with a Quick Start guide available.

Exposition of Front End Build Systems

Exposition of Front End Build Systems

Frontend build systems are crucial in web development, involving transpilation, bundling, and minification steps. Tools like Babel and Webpack optimize code for performance and developer experience. Various bundlers like Webpack, Rollup, Parcel, esbuild, and Turbopack are compared for features and performance.

Declaratively build your APIs in TypeScript and Python

Declaratively build your APIs in TypeScript and Python

Metatype is a declarative API development platform focusing on creating reliable, modular components. It offers Typegraphs for composing APIs, Typegate for GraphQL/REST, and Meta CLI for live reloading. Users can access documentation and a quick start guide on the Metatype GitHub repository.

Link Icon 13 comments
By @austin-cheney - 3 months
whilst reducing the time to create type definition files from minutes, sometimes even hours, down to less than a second.

WTF. What kind of project would take long to compile from TS? Windows 11 (if it were TS)?

I had a long time personal project that was several megs of code with several hundred types/interfaces. It took 13 seconds to compile. If your application takes hours to compile and is less than a petabyte of source code you have some catastrophic mistakes in your approach.

By @seniorsassycat - 3 months
The blog doesn't explain how to take advantage of isolated modules without using JSR, or how JSR transforms published packages.

How would a project configure node and typescript to use a npm module that has exported .ts files?

By @matthberg - 3 months
The site's down for me, here's an Internet Archive link:

https://web.archive.org/web/20240706220437/https://marvinh.d...

I've really enjoyed this author's series on optimization techniques explored through non-trivial yet still small case studies, the other articles linked near the top are all worth checking out too!

By @_jackwink - 3 months
This article is kind of a messy read, isolated declarations feels like it's being used as a mcguffin to advertise the jsr registry. Both may be neat things on their own, but I feel the connection is a bit misleading.

Isolated declarations should allow parallelization and faster type checking with tooling that supports it. This shouldn't be changing your build/release process or what you export in your package.json.

If you also happen to use the jsr registry to publish your package, it sounds like you can update your package.json to export TS files and they'll compile/inject the JS into your release artifacts on publish. Not sure if this feature requires isolated declarations though.

By @ericyd - 3 months
This doesn't really seem to address my most common packaging pain points in the JS/TS ecosystem (which is the inability to easily use ESM in legacy commonjs applications) but maybe I'm just working in the wrong area?

I think the author seriously exaggerates the difficulty of creating type declaration files. It's literally one boolean setting in your tsconfig. I wish they would have given more time to proving the alleged pain points with some examples rather than just telling us how great this new feature is. I personally don't see how this changes my life at all, and I'm a full time JS/TS dev.

By @pzmarzly - 3 months
From what I understand, the author is persuading library authors to stop compiling TS->JS, and instead ship your TS files to the users over npm. I like the idea of it, but will it actually work in practice? What if some dependency only compiles with some version of tsc? What if it needs different tsconfig-s?
By @29athrowaway - 3 months
JavaScript runtimes are full of optimizations that execute conditionally on specific scenarios. If you don't follow the rewarding path of optimization you then go to the deeply unhappy path of deoptimization.

It's easy to get started with JavaScript, but to squeeze every bit of performance in JavaScript will quickly get you into a very complex topics that are not documented and change may change from version to version as they are often not "contracts".

The only way to learn those obscure topics is by reading the source code for your JavaScript implementation, their bug tracker, etc. This means if you have not been reading C++ or executed a non-JS profiler to make your JS faster then you probably have not gone far enough.

There are unexpected things that you would never have suspected if you had not read the implementation.

By @viraptor - 3 months
I feel like the point is skipped in the post: if you ship TS sources, it means you need the TS compiler on the installed side, right? And specifically the right TS version for each installed package... which in some cases may mean conflicting requirements?

(If you own code is in JS, that is)

By @tlarkworthy - 3 months
I don't know what an isolated declaration is. Do we write TS source differently?
By @stevenpetryk - 3 months
I believe the intention behind isolatedModules is to make compilation faster by forcing you to define types that require no inference from other files (thus, being isolated).

Not… whatever this is.

By @rezonant - 3 months
Please do not start publishing raw Typescript into the NPM registry thinking that all the reasons we have not done that in the past do not continue to apply.

I shouldn't have to say this, but at least 27 people upvoted this article, so here we are.

isolatedDeclarations is most useful for large codebases and other situations where performing a full Typescript compilation is prohibitively expensive. This is not the vast majority of cases. It is the declarations analog of transpileOnly, which skips all type checks and simply strips Typescript's special syntax.

The author seems to assume use of JSR, which is an alternative to the NPM registry which automatically compiles Typescript. Not being terribly familiar with it, it's possible this is more relevant to users of that registry, but it's not clear why this is an issue when the registry itself is handling transpiling and declaration generation.

EDIT: To be clear, I think it's totally fine to publish your TS source in addition to your JS and d.ts outputs, just don't publish TS only packages, please.

By @rty32 - 3 months
...wait.

Is the entire article based on the assumption that everyone has Typescript installed in every project? Otherwise, how would publish Typescript files work at all?

> We only ever ship build artifacts, the compiled JS and the relevant .d.ts files in a package.

Isn't this because anyone, regardless of whether they have use the project in JS or TS, can import the library without caring out how the library code is implemented (in JS or TS)? It is not perfect but has worked for almost everyone. How would the author's project layout help people who only ever write code in JavaScript and never installed the Typescript package? Does Typescript have to be a dev dependency of any project that has an upstream package written in Typescript? That seems a bad idea.

I write almost all my JavaScript projects with Typescript now (it's a bad sentence but you know what I mean), but I don't think we should ever make the life harder for those who only write in JavaScript.

By @rtsao - 3 months
I've heard of `isolatedDeclarations` in TS 5.5, but this post left me with more questions than answers because it conflates so many distinct things (TS 5.5, Deno, JSR).

I'll try my best to break it down succinctly:

1) Historically, .d.ts generation is slow because it requires the full TypeScript type checker in order to correctly handle all cases (e.g. exported functions with inferred return types)

2) However, if types were to be fully explicit, then .d.ts generation could be performed without a full type checker (i.e. by a faster compiler via mere syntax transformation)

3) The isolatedDeclarations flag causes the TS compiler to trigger errors when any exports would require inference in order to generate .d.ts

4) Thus, the isolatedDeclarations flag can be used to guarantee compatibility with faster, simpler .d.ts generation using syntax-based transformation

5) A consequence of simpler .d.ts generation is that it can be trivially done in parallel, which can be helpful for large monorepos