isaacs/tshy

Conundrum with relative imports

tmcw opened this issue · 2 comments

tmcw commented

Hi! This is kind of a bug report, but maybe it's for TypeScript, or a help report - I'm not sure, mostly just a conundrum!

So I started on a new module that I want to be consumable by third-party code. Maybe it should be pure-ESM but anyway, I started with tshy, and the default tsconfig you get from tsc --init:

{
  "compilerOptions": {
    "declaration": true,
    "declarationMap": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "inlineSources": true,
    "jsx": "react",
    "module": "nodenext",
    "moduleResolution": "nodenext",
    "noUncheckedIndexedAccess": true,
    "resolveJsonModule": true,
    "skipLibCheck": true,
    "sourceMap": true,
    "strict": true,
    "target": "es2022"
  }
}

And I have a project layout like

src
├── diagnostics
│   └── utils.ts
└── index.ts

And index.ts looks like:

export * from "./diagnostics/utils.ts";

diagnostics/utils.ts just has a few utils functions, nothing that relevant as far as I can tell. So, this code causes TypeScript to complain while running tshy:

src/index.ts:1:15 - error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.

1 export * from "./diagnostics/utils.ts";
                ~~~~~~~~~~~~~~~~~~~~~~~~

So, I enable allowImportingTsExtensions:

error TS5096: Option 'allowImportingTsExtensions' can only be used when either 'noEmit' or 'emitDeclarationOnly' is set.

Found 1 error.

Which brings me to this issue which notes that noEmit has to be falsy. If I set emitDeclarationOnly then it does what it says on the tin and only emits declarations, which isn't very useful.

Maybe I shouldn't be using relative imports in this project and should only use root-relative imports, or a baseDir setting? I'm not really sure how to get to square 2, and import anything from a file in my project, if nodenext wants me to use .ts in imports but I can't enable the setting that allows me to do that. Maybe just looking at the problem incorrectly?

Thanks! -Tom

You just need to change the extension in the export to .js:

export * from "./diagnostics/utils.js";

More info can be found here: https://www.typescriptlang.org/docs/handbook/modules/reference.html

isaacs commented

Yeah, tbh, just accept that your import/export statements relate to the results, not the inputs, and it all just works. No fancy configs needed, you can even delete tsconfig.json and let tshy create it for you.