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

后端 未结 2 1771
野的像风
野的像风 2021-01-22 10:18

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 i

相关标签:
2条回答
  • 2021-01-22 11:04

    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"
    ]
    
    0 讨论(0)
  • 2021-01-22 11:13

    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.

    0 讨论(0)
提交回复
热议问题