Skip to content

Rewrite import specifiers to use .ts extensions, remove tsx#1548

Merged
webpro merged 11 commits intowebpro-nl:mainfrom
wojtekmaj:ts-import-specifier
Feb 22, 2026
Merged

Rewrite import specifiers to use .ts extensions, remove tsx#1548
webpro merged 11 commits intowebpro-nl:mainfrom
wojtekmaj:ts-import-specifier

Conversation

@wojtekmaj
Copy link
Copy Markdown
Contributor

@wojtekmaj wojtekmaj commented Feb 19, 2026

Follow-up after discussion in #1541

Likely to fail on older versions of Node (<22.18.0) and/or TypeScript (<5.7), will have to figure out what to do with tests (prebuild on newer version before running on older one? freezing this PR until Node.js 20 is dropped?)

Script used for mass rewrite
node <<'NODE'
const fs=require('fs');
const path=require('path');
const exts=['.ts','.tsx','.mts','.cts'];
function walk(dir, out=[]){
  for(const e of fs.readdirSync(dir,{withFileTypes:true})){
    if(e.name==='node_modules' || e.name==='.git' || e.name==='dist') continue;
    const p=path.join(dir,e.name);
    if(e.isDirectory()) walk(p,out);
    else if(e.isFile() && p.endsWith('.ts') && "'!p.endsWith('"'.d.ts')) out.push(p);
  }
  return out;
}
const files=walk('.');
const re=/(?:from\\s*['"]|import\\s*['"]|import\\s*\\(\\s*['"]|require\\s*\\(\\s*['"])(\\.{1,2}\\/["'^'"'"\\n?#]+?)\\.js(?=['"])/g;
const hits=[];
for(const file of files){
  const text=fs.readFileSync(file,'utf8');
  let m;
  while((m=re.exec(text))){
    const rel=m[1];
    const base=path.resolve(path.dirname(file),rel);
    let hasTs=false;
    for(const ext of exts){
      if(fs.existsSync(base+ext)){hasTs=true;break;}
    }
    if(hasTs) hits.push(file);
  }
}
console.log([...new Set(hits)].sort().join('\\n'));
NODE

Copilot AI review requested due to automatic review settings February 19, 2026 10:08
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR converts import specifiers from using .js extensions to .ts extensions throughout the TypeScript codebase. The change modernizes the import system to leverage TypeScript 5.7's native support for .ts extensions with the rewriteRelativeImportExtensions compiler option, removing the need for runtime extension rewriting.

Changes:

  • Updated all relative import statements to use .ts extensions instead of .js
  • Modified TypeScript configuration to enable allowImportingTsExtensions and rewriteRelativeImportExtensions
  • Removed tsx dependency from package.json in favor of native Node.js execution
  • Updated plugin generation scripts to output imports with .ts extensions

Reviewed changes

Copilot reviewed 300 out of 744 changed files in this pull request and generated no comments.

Show a summary per file
File Description
packages/knip/src/**/*.ts Changed all relative imports from .js to .ts extensions
packages/knip/scripts/tsconfig.json Added compiler options for TypeScript 5.7's .ts extension support
packages/knip/package.json Removed tsx dependency and updated test scripts to use native Node
packages/docs/package.json Updated build script to use Node instead of tsx
packages/knip/scripts/generate-plugin-defs.js Modified to generate imports with .ts extensions

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Feb 19, 2026

Open in StackBlitz

npm i https://pkg.pr.new/knip@1548
npm i https://pkg.pr.new/@knip/language-server@1548
npm i https://pkg.pr.new/@knip/mcp@1548

commit: 7418338

@webpro webpro force-pushed the ts-import-specifier branch from 568c207 to 0a71633 Compare February 20, 2026 06:12
@webpro
Copy link
Copy Markdown
Member

webpro commented Feb 20, 2026

We could leave in tsx to run .ts test files and... restore .js to build with ts 5.0.4?

@wojtekmaj
Copy link
Copy Markdown
Contributor Author

@wojtekmaj
Copy link
Copy Markdown
Contributor Author

We could leave in tsx to run .ts test files and... restore .js to build with ts 5.0.4?

So, like... Rolling back all the changes? Yeah that's what I was afraid of when thinking about Node.js 18 and thought we might wanna wait and just try a PoC :D

@webpro
Copy link
Copy Markdown
Member

webpro commented Feb 20, 2026

No I mean only like I did in the TS 5.0.4 pipeline, it's all green now

@webpro
Copy link
Copy Markdown
Member

webpro commented Feb 20, 2026

Oh wait dumb oversight on my end. Would still publish with .ts everywhere 🙈

@webpro
Copy link
Copy Markdown
Member

webpro commented Feb 21, 2026

Looks like rewriteRelativeImportExtensions properly rewrites both file extensions and import specifiers (to build using tsc and publish). I think we're good to go actually. WDYT? Am I missing something?

@wojtekmaj
Copy link
Copy Markdown
Contributor Author

wojtekmaj commented Feb 21, 2026

Looks like rewriteRelativeImportExtensions properly rewrites both file extensions and import specifiers (to build using tsc and publish). I think we're good to go actually. WDYT? Am I missing something?

Yeah, this option will indeed do exactly what we want. I just need to resolve conflicts carefully...

@webpro webpro merged commit 58674ad into webpro-nl:main Feb 22, 2026
30 checks passed
@webpro
Copy link
Copy Markdown
Member

webpro commented Feb 22, 2026

Let's go! Thanks a bunch.

@wojtekmaj wojtekmaj deleted the ts-import-specifier branch February 22, 2026 08:18
@webpro
Copy link
Copy Markdown
Member

webpro commented Mar 7, 2026

🚀 This pull request is included in v5.86.0. See Release 5.86.0 for release notes.

Using Knip in a commercial project? Please consider becoming a sponsor.

@wojtekmaj wojtekmaj restored the ts-import-specifier branch March 20, 2026 08:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants