ts-api-utils icon indicating copy to clipboard operation
ts-api-utils copied to clipboard

🚀 Feature: Use namespace import from typescript, not default import

Open JoshuaKGoldberg opened this issue 9 months ago • 2 comments

Bug Report Checklist

  • [x] I have pulled the latest main branch of the repository.
  • [x] I have searched for related issues and found none that matched my issue.

Overview

Coming over from #588: there's interest in switching from default-importing TypeScript to namespace-importing:

- import ts from "typescript";
+ import * as ts from "typescript";

Prettier's https://github.com/prettier/prettier/blob/96d2fe439dcf784806d6543c422fe6ffe679ca4c/scripts/build/config.js#L278 is an example of downstream consumers that have to do work to manually edit files. They do so because tree shaking works better for namespace imports apparently.

Speaking of which, @fisker I have a vague memory of seeing an explanation but now can't find it. Why does a default import not work well for tree-shaking? Are there tracking issues for tools to support it?

Additional Info

TypeScript only added support for a named import in https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-5.html#easier-api-consumption-from-ecmascript-modules. I think this is blocked on ts-api-utils no longer supporting earlier TypeScript versions.

💖

JoshuaKGoldberg avatar Mar 19 '25 13:03 JoshuaKGoldberg

Thanks for this issue.

Good to know that only typescript>=5.5 supports it.

Since typescript is a CommonJS module, it doesn't matters for other users, at least ESBuild doesn't support tree-shaking CommonJS module, Prettier was able to tree shake it because we have another script transform it into ESM.

I have a vague memory of seeing an explanation but now can't find it.

I don't have an explanation, but using named/namespace export did produce a smaller file.

As I understand main purpose of this module is to serve typescript-eslint, I guess we can't drop support for typescript<5.5 now, I guess I'll have to close https://github.com/JoshuaKGoldberg/ts-api-utils/pull/588

Thanks anyway.

fisker avatar Mar 19 '25 14:03 fisker

👍 marking as blocked. Even without typescript-eslint it's likely we'd want to target at least the same TypeScript support window as DefinitelyTyped: https://github.com/DefinitelyTyped/DefinitelyTyped?tab=readme-ov-file#support-window. That has 5.4 going out of support in 2026-03.

JoshuaKGoldberg avatar Mar 20 '25 12:03 JoshuaKGoldberg