Repository navigation
Typing not being loaded correctly #1359
Description
Custom typings (own path in "typeRoots") are not being loaded when i'm using @mailchimp/mailchimp_marketing - they provide a types.d.ts file which only defines a OpenAPI namespace (and no module) so ts-node just errors saying that types.d.ts is not a module - tsc however also parses my custom typing and correctly identifies the module declaration from my custom index.d.ts file.
Expected Behavior
index.d.ts to be loaded even if there is a types.d.ts in node_modules/@mailchimp/mailchimp_marketing/
Actual Behavior
Only types.d.ts is loaded, the index.d.ts in my custom typings/ folder is ignored. Removing types.d.ts from node_modules/@mailchimp/mailchimp_marketing/ resolves the issue. tsc correctly resolves both types.d.ts and index.d.ts
Steps to reproduce the problem & minimal reproduction
Add a tsconfig with typeRoots specified as per https://github.com/TypeStrong/ts-node#help-my-types-are-missing and then do:
npm i @mailchimp/mailchimp_marketing # this package provides a types.d.ts but only defines a namespace, not declaring a module
mkdir -p typings/@mailchimp/mailchimp_marketing
echo "declare module '@mailchimp/mailchimp_marketing';" > typings/@mailchimp/mailchimp_marketing/index.d.ts
mkdir src
echo "import client from '@mailchimp/mailchimp_marketing'" > src/index.ts
./node_modules/.bin/ts-node src/index.ts # errors
./node_modules/.bin/tsc # compiles fineSpecifications
- ts-node version: 9.0.0
- node version: 14.15.1
- TypeScript version: 4.0.5
- tsconfig.json, if you're using one:
{
"compilerOptions": {
"target": "ES2020", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
"lib": ["ES2020"], /* Specify library files to be included in the compilation. */
"outDir": "./build", /* Redirect output structure to the directory. */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
"baseUrl": "src/", /* Base directory to resolve non-absolute module names. */
"types": ["node"], /* Type declaration files to be included in compilation. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
"typeRoots": ["./typings", "./node_modules/@types"]
}
}
- Operating system and version: Windows 10 (20H2)
- If Windows, are you using WSL or WSL2?: WSL2
Activity
linde12 commentedon Jun 2, 2021
FYI: Works fine with
--filesbut i'd like to avoid it if possible.blakeembrey commentedon Jun 2, 2021
Does changing the directory name to
mailchimp__mailchimp_marketingwork? IIRC TypeScript looks for the directory the same way it would exist innode_modules, and you can't do@types/@mailchimp/mailchimp_marketingon NPM so they use the__scheme.cspotcode commentedon Jun 3, 2021
I bet if you set
"rootDir": "src"it will also start failing intsc. Which I know sounds like a weird suggestion; "why is he asking me to break it?"But my hunch is that the typings are not actually being loaded via
typeRoots/types. They are being loaded because./typings/is within yourrootDirwhich defaults to..tscandts-node --fileseagerly load everything withinrootDir. This informs it of your@mailchimp/mailchimp_marketingdeclaration, so it never attempts to pull the declaration fromnode_modules.If my hunch is correct, adding a
/// <reference path=above theimport "@mailchimp/mailchimp_marketing"should fix this./// <reference>is how you trigger file inclusion without--files.cspotcode commentedon Jun 3, 2021
Blake's suggestion also sounds smart; but I wanted to offer a backup.