sindresorhus / tsconfig

Shared TypeScript config for my projects
MIT License
346 stars 29 forks source link

module: Preserve #29

Open fregante opened 1 month ago

fregante commented 1 month ago

https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-4.html#support-for-require-calls-in---moduleresolution-bundler-and---module-preserve

I think this is the right config to finally get TS out of the way

{
    "compilerOptions": {
        "module": "preserve",
        // ^ also implies:
        // "moduleResolution": "bundler",
        // "esModuleInterop": true,
        // "resolveJsonModule": true,
        // ...
    }
}

More info in https://github.com/microsoft/TypeScript/pull/56785

sindresorhus commented 1 month ago

What's the benefit for ESM-only projects? Not sure I see the value in it.

fregante commented 1 month ago

I think the benefit is to stop fighting TypeScript with imports: just let the user use what they write. Specific rules/preferences can be enforced via eslint.

Or at least that’s the promise

sindresorhus commented 1 month ago

There is really no fighting if you are pure ESM. And I don't really see the point in allowing require here and then use a linter to disallow it, when it can just be disallowed here instead.

fregante commented 1 month ago

There is really no fighting if you are pure ESM

I reached this point because this config blocks import attributes AKA “pure ESM”, so the fight still exists. Then node wants .ts imports and TS wants .js imports. The fight continues.

If preserve keeps its promise to just allow modules as written, I think it’s advantageous.

fregante commented 1 month ago

This is what I see with @sindresorhus/tsconfig 6, without changes: ESM is not allowed, require is:

Screenshot 13
sindresorhus commented 1 month ago

If preserve keeps its promise to just allow modules as written, I think it’s advantageous.

I suggest trying it out in practice first on some popular projects to see its effects. I'm already lukewarm to it, but I'm definitely not adding it here until it's proven to work.

sindresorhus commented 1 month ago

This is what I see with @sindresorhus/tsconfig 6, without changes: ESM is not allowed, require is:

That's just the fact of using TS in general. Every JS feature has to be supported by TS before you can use it there. I thought TS already supported import attributes: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-3.html

fregante commented 1 month ago

I thought TS already supported import attributes

They do, but you have to use a specific config (and this config particularly blocks resolveJsonModule because Node <18.20 doesn't support it)