简体   繁体   English

Typescript:使用 dts-gen 后找不到模块的声明文件

[英]Typescript: could not find a declaration file for module after using dts-gen

I am using an npm package, https://github.com/SymphonyPlatformSolutions/symphony-api-client-node , that doesn't have types associated.我正在使用 npm package, https://github.com/SymphonyPlatformSolutions/没有关联的类型,

Directory structure目录结构

node_modules
  symphony-api-client-node/
    lib
      ...
src
  symphony-adapter.ts
package.json
tsconfig.json

I tried to use dts-gen to create a type file, via dts-gen -m symphony-api-client-node .我尝试通过dts-gen -m symphony-api-client-node使用 dts-gen 创建类型文件。 When I add this file, symphony-api-client-node.d.ts into src, I still receive an error for no declaration file for module symphony-api-client-node .当我将这个文件symphony-api-client-node.d.ts到 src 中时,我仍然收到一个错误,因为没有模块symphony-api-client-node声明文件。

Based on https://www.typescriptlang.org/docs/handbook/module-resolution.html#how-typescript-resolves-modules , it should see the src/symphony-api-client-node.d.ts file and use that - is there something I'm missing here?基于https://www.typescriptlang.org/docs/handbook/module-resolution.html#how-typescript-resolves-modules ,它应该看到src/symphony-api-client-node.d.ts文件并使用那-我在这里缺少什么吗?

tsconfig.json tsconfig.json

{
    "compilerOptions": {
        "module": "commonjs",
        "allowJs": true,
        "esModuleInterop": true,
        "target": "es2017",
        "noImplicitAny": true,
        "moduleResolution": "node",
        "sourceMap": true,
        "outDir": "dist",
        "removeComments": true,
        "jsx": "react"
    },
    "include": ["src/**/*", "config/*"],
    "exclude": ["./node_modules", "**/tests"]
}

symphony-adapter.ts交响乐适配器.ts

import Symphony from 'symphony-api-client-node';

class Adapter {
  constructor(symphony: Symphony){
    this.symphonyBase = symphony;
  }
}

Error:错误:

src/SymphonyAdapter.ts:3:22 - error TS7016: Could not find a declaration file for module 'symphony-api-client-node'. 'path/node_modules/symphony-api-client-node/index.js' implicitly has an 'any' type.
  Try `npm install @types/symphony-api-client-node` if it exists or add a new declaration (.d.ts) file containing `declare module 'symphony-api-client-node';`

3 import Symphony from 'symphony-api-client-node';

symphony-api-client-node.d.ts交响乐-api-client-node.d.ts

/** Declaration file generated by dts-gen */

export function authenticateBot(SymConfig: any): any;

export function authenticateExtApp(): any;

export function authenticateOboApp(): any;

export function createRoom(room: any, description: any, keywords: any, membersCanInvite: any, discoverable: any, anyoneCanJoin: any, readOnly: any, copyProtected: any, crossPod: any, viewHistory: any): any;

export function createSignal(name: any, query: any, visibleOnProfile: any, companyWide: any, sessionToken: any): any;
...

Wrap the contents of the generated symphony-api-client-node.d.ts file with将生成的symphony-api-client-node.d.ts文件的内容用

declare module "symphony-api-client-node" { }

For example: symphony-api-client-node.d.ts例如:symphony-api-client-node.d.ts

declare module "symphony-api-client-node" {
  export function acceptConnectionRequest(userId: any, sessionToken: any): void;
  export function activateRoom(streamId: any): void;
  export function addMemberToRoom(streamId: any, userId: any): any;
  ...
}

Be aware that dts-gen is a starting point;请注意, dts-gen是一个起点; you'll need to do some work to fix types (and the Readme for dts-gen says as much).您需要做一些工作来修复类型( dts-gen的自述文件也说了这么多)。 For example, while many of the exported functions in symphony-api-client-node return promises, the generated .d.ts file will indicate return type any for most, if not all of them.例如,虽然symphony-api-client-node中的许多导出函数返回承诺,但生成的.d.ts文件将指示大多数(如果不是全部)返回类型为any

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 打字稿:找不到模块“react-cards”的声明文件 - Typescript: Could not find a declaration file for module 'react-cards' 在 TypeScript 中使用 SweetAlert2,找不到模块“sweetalert2/dist/sweetalert2.js”的声明文件 - Using SweetAlert2 with TypeScript, could not find a declaration file for module 'sweetalert2/dist/sweetalert2.js' Cloudinary-React 找不到模块“cloudinary-react”的声明文件 - 不使用 TypeScript - Cloudinary-React Could not find a declaration file for module 'cloudinary-react' - Not using TypeScript TypeScript 错误“找不到模块的声明文件”-无法修复 - TypeScript error "Could not find a declaration file for module" - unable to fix 找不到模块的声明文件 - Could not find a declaration file for module 无法找到模块'drivelist'的声明文件 - Could not find a declaration file for module 'drivelist' 找不到模块“mymodule”的声明文件 - Could not find a declaration file for module 'mymodule' 找不到模块“react”的声明文件 - Could not find a declaration file for module 'react' 在反应中找不到模块错误的声明文件 - Could not find a declaration file for module error in react 找不到模块“@editorjs/image”的声明文件 - Could not find a declaration file for module '@editorjs/image'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM