#ESM imports with query parameters

12 messages · Page 1 of 1 (latest)

sudden folio
#

Hello, I'm trying to use ESM style relative file imports with query parameters in my code. I've read through the Modules Reference which suggests "The TypeScript compiler recognizes standard ECMAScript module syntax in TypeScript" and I didn't find any explicit mention of query parameters in imports not being supported. But I haven't been able to find a set of configuration options that seem to make it work.

For example my code looks like:

// @filename: dep.ts
export const f = () => {console.log("hi")};

// @filename: entry.ts
import { f } from "./dep.js?query=value";
f();

I'm using target: ESNext, module/moduleResolution: NodeNext. Attempting to compile errors with: entry.ts:1:19 - error TS2307: Cannot find module './dep.js?query=what' or its corresponding type declarations.. Renaming the files to .js and running directly with node does succeed. It seems like Typescript isn't fully modeling node's resolution in this situation. Is that expected?

ember sparrow
sudden folio
ember sparrow
#

And a comment under this article:

#

Could you try if this works for you

#

@sudden folio

sudden folio
#

Ah that works but it's pretty annoying since I have to list out every potential query param and value (and combination of multiple params).

ember sparrow
#

You can update the path mapping according to your needs
Something like this:

#
"paths": {
  "./*?*": "./*"
}

Not even sure if it is the right syntax 😅

#

On second thought, this might not work because of the use of 2 wildcards