【发布时间】:2020-11-27 01:44:50
【问题描述】:
我正在使用一个 npm 包 https://github.com/SymphonyPlatformSolutions/symphony-api-client-node,它没有关联类型。
目录结构
node_modules
symphony-api-client-node/
lib
...
src
symphony-adapter.ts
package.json
tsconfig.json
我尝试使用 dts-gen 通过dts-gen -m symphony-api-client-node 创建类型文件。当我将这个文件 symphony-api-client-node.d.ts 添加到 src 中时,我仍然收到一个错误,因为没有模块 symphony-api-client-node 的声明文件。
基于https://www.typescriptlang.org/docs/handbook/module-resolution.html#how-typescript-resolves-modules,它应该会看到src/symphony-api-client-node.d.ts 文件并使用它——这里有什么我遗漏的吗?
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"]
}
交响乐适配器.ts
import Symphony from 'symphony-api-client-node';
class Adapter {
constructor(symphony: Symphony){
this.symphonyBase = symphony;
}
}
错误:
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
/** 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;
...
【问题讨论】:
-
你能告诉我们它产生了什么吗?
-
在主帖@AluanHaddad 中更新 - 它看起来像一个非常标准的 TS 文件
-
感谢您添加文件。它没有任何问题,但 typescript 并没有在 src 下寻找第三方类型声明。您可以使用路径或 typeRoots 编译器选项来通知 typescript 这些声明与您正在导入的模块相关联
标签: javascript typescript types node-modules