How to add a new index signature to a class declaration from file.d.ts in typescript?

I have a class like the following:

// some npm module
export class User {
  fname: string;
  lname: string;
}

But I cannot update the declaration of this class from my project root because it is an npm module. I want to add a new index signature to this class but am unable to do so! I tried the following code, but it doesn't work!

// file.d.ts
export declare class User {
  customProperty: any;
}

Solution 1:

You don't need to export the declaration, all you need to in your file.d.ts is to do something like this,I'll use axios as an example here:

declare module "axios" {
  interface AxiosRequestConfig {
    customProperty: "some-type";
  }
  interface AxiosInstance {
    request<T = any>(config: AxiosRequestConfig): Promise<T>;
    get<T = any>(url: string, config?: AxiosRequestConfig): Promise<T>;
    delete<T = any>(url: string, config?: AxiosRequestConfig): Promise<T>;
    head<T = any>(url: string, config?: AxiosRequestConfig): Promise<T>;
    options<T = any>(url: string, config?: AxiosRequestConfig): Promise<T>;
    post<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T>;
    put<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T>;
    patch<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T>;
  }
}

for more information read module augmentation in typescript docs: here is the link