How to allow modules that have missing .d.ts type definition?
Asked Answered
L

2

5

I'm using some unpopular modules such as Dyo and js-sha3 which doesn't have any types, it seems.

I don't really care right now about types in third-party libraries, I don't want to spend hours typing those. I mainly use it for the server to limit my mistakes and make troubleshooting easier while in development.

I had a Cannot find module X error before so I've added: "moduleResolution": "node" in my tsconfig.json file.

How can I make typescript not complaining about node modules missing types?

The whole error:

Could not find a declaration file for module 'dyo'. '/node_modules/dyo/dist/dyo.umd.js' implicitly has an 'any' type.
  Try `npm install @types/dyo` if it exists or add a new declaration (.d.ts) file containing `declare module 'dyo';` [7016]

I found // @ts-nocheck in a comment in some dark place of the web but it doesn't seem to work.

Can I turn this off by modules?

Libelous answered 13/1, 2019 at 22:33 Comment(0)
Q
9

You can create dummy .d.ts definition which will allow to import anything from the module - importing it will result in dyo having any type (the exact way to import it depends on the esModuleInterop compiler setting, and on how the things are exported from dyo.umd.js).

  1. create a file dyo.d.ts somewhere within your project with one line in it

    declare module 'dyo';
    
  2. in a file that references that module, add this line at the top, with appropriate path to dyo.d.ts file:

    /// <reference path='./dyo.d.ts' />
    

An alternative to 2. is to add dyo.d.ts to the list of the files included in the compilation in tsconfig.json file:

"files": [
    ... other files as necessary ...

    "./path/to/dyo.d.ts"
]
Quadriga answered 14/1, 2019 at 0:14 Comment(1)
I did go with the files approach to avoid modifying modules files that may break during updates later. Thanks!Libelous
M
2

For whoever does not want to create their own .d.ts definition files, you can use "noImplicitAny": false in tsconfig.json to allow using external libraries without TypeScript definition files.

Example :

{
  "compileOnSave": false,
  "compilerOptions": {
     ...
    "noImplicitAny": false,
     ...
    }
  }
}

This allows TypeScript to infer any type from every missing type, hence this will also affect your own project.

Mccorkle answered 12/1, 2021 at 15:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.