Live data from Hacker News

TypeScript NPM Packages Done Right

blog.liblab.com

41–50 of 81 posts

Re: TypeScript NPM Packages Done Right

#41
> Will it work with ES Modules (ESM) and CommonJS (CJS) style imports?

It's so frustrating that in 2023 this is still a concern. It's probably a bad idea when we adopt the latest JS language standards before they are implemented in the runtime env. Transpiling sucks.

Re: TypeScript NPM Packages Done Right

#42
post #39
post #24

Earlier quoted context omitted.

> much better solutions like SWC Which doesn't support const enum correctly and god knows what else without erroring.

Esbuild works fine and is also much faster than TSC. Also TS enums are flawed: https://blog.logrocket.com/why-typescript-enums-suck/

Not that I care, but from your article:

> Enums in TypeScript are a very useful addition to the JavaScript language when used properly.

So, just like any language feature.

Re: TypeScript NPM Packages Done Right

#44

> Will it work with ES Modules (ESM) and CommonJS (CJS) style imports? It's so frustrating that in 2023 this is still a concern. It's probably a bad idea when we adopt the latest JS language standards before they are implemented in the runtime env. Transpiling sucks.

The problem is there isn't one runtime env, there are many.

On top of that, being implemented in at least two different environments is a prerequisite to get finalized as a standard per the tc39 process.

I agree with you when it comes to things that aren't far along- the decorator goat rodeo is a good example, but on the whole transpiling is essentially a part of the process now.

Re: TypeScript NPM Packages Done Right

#45

Maybe not related, but if a npm package has conditional exports, what's current best way to patch that package.json ? I want to replace its condition exports with mine.

If you use Yarn, there’s the `yarn patch` command [1], which lets you maintain patches for your dependencies. Even though I try to upstream patches wherever possible, sometimes you just want to apply a quick patch and move on, especially if the dependency is poorly maintained or even worse, deeply nested in your dependency hierarchy. I use `yarn patch` regularly, it’s one of the main reasons why I moved to Yarn in the first place.

If you’re not using Yarn, there seems to be a similar thing on npm, `patch-package`. [2] I never had to use that though.

[1]: https://yarnpkg.com/cli/patch

[2]: https://www.npmjs.com/package/patch-package

Re: TypeScript NPM Packages Done Right

#46

Perhaps I’m missing something, but when I’ve done this in the past, I get weird package resolution because my index.js isn’t in the root (`npm publish` publishes the root and doesn’t support alternate paths to my knowledge) - in the end people have to `import x from “mylib/dist”`. I got round this with some funky step where I copy the package.json into the dist folder and rewrite some paths. Is there a better way to…

You can use subpath exports like paulddraper suggested or in your `main` key in `package.json` you just point to whatever file you want to be served when `import X from 'Y'`.

It doesn't have to be `index.js` in the root of your package. That's the fallback if no `main` or subpath exports exist in `package.json`

Re: TypeScript NPM Packages Done Right

#47

`tsc` is extremely slow to transpile TypeScript sources into JavaScript. It's invaluable for typechecking, but for transpilation there are much better solutions like SWC or tsup [1] (which nicely wraps esbuild and Rollup). [1] https://tsup.egoist.dev/

Why do you need it to be fast?

Re: TypeScript NPM Packages Done Right

#48
I'm wondering if there's a tool that converts TypeScript into JavaScript that has JsDoc type annotations and preserves API docs? Then you wouldn't need to include anything else. Let the application minify if they choose.

Re: TypeScript NPM Packages Done Right

#49

`tsc` is extremely slow to transpile TypeScript sources into JavaScript. It's invaluable for typechecking, but for transpilation there are much better solutions like SWC or tsup [1] (which nicely wraps esbuild and Rollup). [1] https://tsup.egoist.dev/

Why do you need it to be fast?

Is this a troll question or do you test and publish your code only every Friday?

Re: TypeScript NPM Packages Done Right

#50
post #14

While the article is technically correct it avoids the most common issue with 'pure' typescript libraries in that you still need a bundler if you have multiple .ts files. Once you enter that territory you realize how much more complex everything becomes.

You don't necessarily need a bundler. You could use index.ts files to re-export all the exports of your source files.

I don't see how that changes the problem that OP describes.

TS module resolution looks almost like ESM, but neither the syntax nor the semantics are the same.

And or course you need a compiler to transpile TS, even if you wish to ignore the typings (often wrapped in a bundler like Vite which in turn wraps swc or ESBuild... because tsc is not as practical for large projects)

Really, having dealt with this kind of problem on Friday, it can make you go crazy.

E.g. having to deal with CJS-specific settings for some tool in a project using TS and exporting to ESM JS...

Post reply on HN