#Do I need to specify moduleResolution for type-checking?

15 messages · Page 1 of 1 (latest)

pearl juniper
#
{
  "compilerOptions": {
    "checkJs": true,
    "target": "ESNext",
    "module": "NodeNext"
  },
  "include": ["."]
}

I have a node javascript project for Node 20 and I only want to use TypeScript for type-checking with above jsconfig.json

Do I need to specify moduleResolution option explicitly or does TypeScript will infer the module resolution system from target and module options in jsconfig.json?

limber canopy
#

all options have defaults, and some are based on other values
these are all documented in the tsconfig documentation

#

moduleResolution is dependent on module

#

(target is irrelevant to mldule system, it refers to what environment you're targeting in terms of es standards)

pearl juniper
limber canopy
#

you could explicitly specify it yourself, nothing wrong with that

pearl juniper
# limber canopy you could explicitly specify it yourself, nothing wrong with that

I mean it is a bit hard to determine what value to use. Say if the project is based on ESM modules that will run in browser environment only, what should be moduleResolution? It seems that best option might be "ESNext" but there is no value related to ESM in suggested options of moduleResolution https://www.typescriptlang.org/tsconfig/#moduleResolution So if possible I could leave it to TS to choose whichever system it sees fit. But some AI answers suggests moduleResolution should be set explicitly. I am a bit confused.

limber canopy
#

browser projects typically use bundlers afaik

pearl juniper
limber canopy
#

im not sure ts would be able to resolve that correctly at all without specifying paths

harsh cypress
#

For a very simple project with just say, relative imports, they will probably all work

#

They should all incorporate the standard spec, and just add additional capabilities, atleast if they are modern

#

The docs suggest that if the module is ES6, then it will use "classic", but also says that "classic" should not be used. So I don't know, but I also imagine it won't be hard to find one that works.

pearl juniper
# harsh cypress The docs suggest that if the module is ES6, then it will use "classic", but also...

Thank you very much. Indeed I gone ahead and checked the effective configuration taking place by npx tsc -p jsconfig.json --showConfig when module:ESNext as you said moduleResolution is set as "classic" by TS automatically. But as you noted it is not recommended in official docs, though why TS sets it to classic I wonder. Although classic even seems to work for ES6 browser projects. So I think I should not touch the moduleResolution much and leave it to TS.