Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding properties to an existing type with TypeScript

So I have a library written in TypeScript, which uses the NPM depenedency 'ldapjs'.

I also have @types/ldapjs installed into my project.

So in my project, I have this:

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

Now here is my question - how can I add properties to a client with type Client?

I have this:

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

where IClient is my version of an ldapjs Client with a few extra properties.

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

but the problem is, if I try to add properties to client, I get this error:

'cdtClientId' property does not exist on type Client.

Is there some way I can cast Client to IClient?

How can I do this? I have this same problem in many different projects.

like image 828
Alexander Mills Avatar asked Sep 18 '25 22:09

Alexander Mills


1 Answers

While what you have added as answer might solve your problem, you can directly augment the Client interface and add the properties. You don't even need IClient. You can use module augmentation:

declare module "ldapjs" {
    interface Client {
        // Your additional properties here
    }
}
like image 70
Saravana Avatar answered Sep 20 '25 14:09

Saravana