iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
😺

Distributing TypeScript without compilation for internal modules

に公開1件

Claim

In this day and age, I believe there is almost no need to compile and distribute TypeScript, except for things published to npm.

My intuition is that using environments that don't require TypeScript compilation, such as Deno or Rome, significantly improves the development experience by skipping the compilation step. I believe this benefit should be prioritized in local environments.

My main point is that, basically, a build should only be necessary once at the very end before distribution, for both frontend and server.

https://github.com/denoland/deno

Reason

According to modern npm practices, placing things that depend on TypeScript into the npm registry is considered bad practice. However, for internal modules in a monorepo or a private GitHub Package Registry used within a specific company, that restriction shouldn't apply as long as consensus is reached among the users.

packages/
  foo/
    index.ts
    package.json
  bar/
    index.ts
    package.json
tsconfig.json
package.json

(In this case, it is also possible to specify "main": "index.ts" in the main field of foo's package.json.)

TypeScript's specifications have become quite stable. Various TypeScript compilers have emerged—such as Babel's preset-typescript, esbuild used in Vite, swc used in Deno, and less common ones like sucrase. As these have moved into production use, a consensus on what constitutes the "TypeScript baseline" has more or less been established.

Microsoft's TypeScript itself rarely breaks backward compatibility. Furthermore, in modern Node.js or browser environments at the ES2021 level, often only the parts that strip TypeScript types are used, reducing the emergence of transpilation-induced "dialects."

Even when unavoidable IE support is required, it's often handled by a multi-stage babel-loader separate from ts-loader, so ES5 transpilation is not strictly required as a responsibility of TypeScript.

If types break on the module consumer side due to differences in tsconfig strictness, you can always ignore it using "skipLibCheck": true as a last resort.

Benefits of Distributing TypeScript As-Is

  • No need to rebuild to reflect changes in the module
  • No need for the somewhat tedious .d.ts build configuration
  • No need for troublesome source map handling
  • Can jump to the implementation instead of the .d.ts type definition in VS Code IntelliSense
  • The consumer's tsconfig can decide whether to build as CommonJS or ESM
  • The consumer can choose their preferred TypeScript compiler

Disadvantages of Distributing TypeScript As-Is

  • If the module uses the latest TypeScript features, the consumer side must also upgrade their version.
  • Compilation errors can occur if tsconfig settings do not align.
  • For Node.js servers, a runtime like esbuild is required.
    • This may cause non-negligible overhead in extremely large projects.
  • For JSX other than React, a JSX pragma is required in every file.
  • You may encounter minor execution "dialects."
  • In environments where TypeScript transpilation is ignored under node_modules/*, you need to forcibly enable it (e.g., Next.js).

Conclusion

It would be nice if the era comes where we can just switch over to Rome/Deno as they are.

Discussion

malt03malt03

良記事ありがとうございます!参考にさせていただいております。

node_modules/* の下で ts のトランスパイルを無視する環境で、無理やり有効化する必要がある (next.js)

これは next-transpile-modules を使ってやっていますか?