Ale*_*lls 4 node.js typescript typescript2.0
所以我有一个用 TypeScript 编写的库,它使用 NPM 依赖项“ldapjs”。
我也已经@types/ldapjs安装到我的项目中了。
所以在我的项目中,我有这个:
import {Client} from '@types/ldapjs';
import * as ldap from 'ldapjs';
Run Code Online (Sandbox Code Playgroud)
现在这是我的问题 - 如何向类型为 Client 的客户端添加属性?
我有这个:
export interface IClient extends Client{
__inactiveTimeout: Timer,
returnToPool: Function,
ldapPoolRemoved?: boolean
}
Run Code Online (Sandbox Code Playgroud)
其中 IClient 是我的 ldapjs 客户端版本,具有一些额外的属性。
let client = ldap.createClient(this.connOpts); // => Client
client.cdtClientId = this.clientId++;
Run Code Online (Sandbox Code Playgroud)
但问题是,如果我尝试向客户端添加属性,则会收到此错误:
客户端类型上不存在“cdtClientId”属性。
有什么方法可以将 Client 转换为 IClient 吗?
我怎样才能做到这一点?我在许多不同的项目中都遇到同样的问题。
虽然您添加的答案可能会解决您的问题,但您可以直接扩展Client界面并添加属性。你甚至不需要IClient。您可以使用模块增强:
declare module "ldapjs" {
interface Client {
// Your additional properties here
}
}
Run Code Online (Sandbox Code Playgroud)