Why does this module resolution and type reference resolution differ?

49 views Asked by At

I was playing with an npm package (tabletojson) which had a dependency on cheerio. I realised that cheerio already comes with associated typings and hence, should not need @types/cheerio. However, to my surprise, running npx tsx --traceResolution resolved the module cheerio to supplied typings but the type reference directive was actually resolving to @types/cheerio.

Why does type resolution happen from @types/cheerio when the package itself supplies its types?

Reference output of npx tsc --traceResolution

  • Module resolution
======== Resolving module 'cheerio' from '/working-directory/tabletojson/lib/Tabletojson.ts'. ========
Explicitly specified module resolution kind: 'Node10'.
Loading module 'cheerio' from 'node_modules' folder, target file types: TypeScript, Declaration.
Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.
Directory '/working-directory/tabletojson/lib/node_modules' does not exist, skipping all lookups in it.
Found 'package.json' at '/working-directory/tabletojson/node_modules/cheerio/package.json'.
File '/working-directory/tabletojson/node_modules/cheerio.ts' does not exist.
File '/working-directory/tabletojson/node_modules/cheerio.tsx' does not exist.
File '/working-directory/tabletojson/node_modules/cheerio.d.ts' does not exist.
'package.json' does not have a 'typesVersions' field.
'package.json' does not have a 'typings' field.
'package.json' has 'types' field 'lib/index.d.ts' that references '/working-directory/tabletojson/node_modules/cheerio/lib/index.d.ts'.
File '/working-directory/tabletojson/node_modules/cheerio/lib/index.d.ts' exists - use it as a name resolution result.
Resolving real path for '/working-directory/tabletojson/node_modules/cheerio/lib/index.d.ts', result '/working-directory/tabletojson/node_modules/cheerio/lib/index.d.ts'.
======== Module name 'cheerio' was successfully resolved to '/working-directory/tabletojson/node_modules/cheerio/lib/index.d.ts' with Package ID 'cheerio/lib/[email protected]'. ========
  • Type reference directive 'cheerio'
======== Resolving type reference directive 'cheerio', containing file '/working-directory/tabletojson/__inferred type names__.ts', root directory '/working-directory/tabletojson/node_modules/@types,/working-directory/node_modules/@types'. ========
Resolving with primary search path '/working-directory/tabletojson/node_modules/@types, /working-directory/node_modules/@types'.
Found 'package.json' at '/working-directory/tabletojson/node_modules/@types/cheerio/package.json'.
'package.json' does not have a 'typesVersions' field.
'package.json' does not have a 'typings' field.
'package.json' has 'types' field 'index.d.ts' that references '/working-directory/tabletojson/node_modules/@types/cheerio/index.d.ts'.
File '/working-directory/tabletojson/node_modules/@types/cheerio/index.d.ts' exists - use it as a name resolution result.
Resolving real path for '/working-directory/tabletojson/node_modules/@types/cheerio/index.d.ts', result '/working-directory/tabletojson/node_modules/@types/cheerio/index.d.ts'.
======== Type reference directive 'cheerio' was successfully resolved to '/working-directory/tabletojson/node_modules/@types/cheerio/index.d.ts' with Package ID '@types/cheerio/[email protected]', primary: true. ========
0

There are 0 answers