ChatGPT解决这个技术问题 Extra ChatGPT

Where should I place custom .d.ts files?

I'm trying to provide typings for the package that does not have them:

error TS7016: Could not find a declaration file for module 'inputmask-core'. './node_modules/inputmask-core/lib/index.js' implicitly has an 'any' type.
Try `npm install @types/inputmask-core` if it exists or add a new declaration (.d.ts) file containing `declare module 'inputmask-core';`

I'm using ts-loader in webpack with typescript 2.4.2, and I have the following type roots set up in tsconfig.json:

"typeRoots": [
  "./node_modules/@types",
  "./src/client/types"
]

I tried to mimic the package structure in node_modules/@types:

src/client/types
|--inputmask-core
  |--index.d.ts

With the following in index.d.ts:

declare class InputMask {}
export default InputMask;

But the error is still there. What am I doing wrong? Where should I place those custom .d.ts files?

And what is the difference between node_modules/@types and any other type root? Why does TypeScript treat them differently?


C
Community

Use paths instead of typeRoots

https://github.com/Microsoft/TypeScript/issues/22217#issuecomment-369783776

https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping

"typeRoots" is meant for global code. i.e. something that is declared in the global namespace, and you want to include it. For modules, they have their own scope, all you need is path mapping.. something like:

{
    "compilerOptions": {
        "target": "es2017",
        "baseUrl": "./",
        "paths": {
            "*": [ "src/client/@custom_types/*"]
        }
    },
    "exclude": [ "node_modules", "src/client/@custom_types", ... ]
}

Note: baseUrl is required with paths, and you probably also want to add the dir to exclude.


specifying the path where the .d.ts file work like a charm
This is so strange. I tried this but It wont budge. I still get the ` TS2307: Cannot find module` error. I have this in my compiler options: "baseUrl": ".", "paths": { "~/*": ["src/*"], "*": ["@custom_types/*"] } but a path like so import icons from './svg/inline/icons.svg'; doesn't work. Any ideas?
R
Rogach

Possible solution: place the following in index.d.ts, and it will compile:

declare module "inputmask-core" {
    declare class InputMask {}
    export default InputMask;
}

I still don't understand the special handling that node_modules/@types gets, though.


node_modules/@types is used to hold the third party packages containing type information for packages that do not come with declarations built in. It gets special handling in that you do not import from it directly, rather each declaration package in it is seen as overlaid onto the package for which it contains types. "typeRoots" lets you customize the location or add additional directories that get treated in this special fashion.
@AluanHaddad - If additional directories in "typeRoots" also get treated in this special way, then why moving directory for some package from node_modules/@types to other typeRoot breaks compilation?
There is not enough information here to tell. No tsconfig, module resolution, etc. What you ended up with is an ambient external module and that has different resolution semantics anyway. In particular, it is not resolved by the path of the file.
mmm, why the hell doesn't dts-gen generate the declare module "foo" wrapper automatically? (I'm following the guide here blogs.msdn.microsoft.com/typescript/2016/12/14/… ) . thanks for the pointer Rogach
s
strider

There is a lot of packages that are not typescript packages, so to fix this error just change compilerOptions.strict to false in tsconfig


This has effects far outside what the OP is asking, and will loosen typing for any source code in the project. Would definitely advise against doing this just to solve the OP's issue.
This is not a good solution