2017-06-15 804 views
0

所以我有一个用TypeScript编写的库,它使用NPM依赖性'ldapjs'。使用TypeScript向现有类型添加属性

我也有@types/ldapjs安装到我的项目中。

所以,在我的项目,我有这样的:

import {Client} from '@types/ldapjs'; 
import * as ldap from 'ldapjs'; 

现在,这里是我的问题 - 怎样才能添加属性到客户端与客户端类型?

我有这样的:

export interface IClient extends Client{ 
    __inactiveTimeout: Timer, 
    returnToPool: Function, 
    ldapPoolRemoved?: boolean 
} 

其中IClient是我有一些额外的属性版本的客户端ldapjs的。

let client = ldap.createClient(this.connOpts); // => Client 
client.cdtClientId = this.clientId++; 

但问题是,如果我尝试将属性添加到客户端,我得到这个错误:

'cdtClientId' property does not exist on type Client.

有一些方法可以让我投客户端IClient?

我该怎么做?我在许多不同的项目中遇到同样的问题。

回答

1

虽然你已经添加的答案可能解决您的问题,您可以直接增加了Client界面和添加的属性。你甚至不需要IClient。你可以使用module augmentation

declare module "ldapjs" { 
    interface Client { 
     // Your additional properties here 
    } 
} 
+0

不错,这很酷我会尝试它 –