Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending a typescript variable declaration

I want to use the nconf-yaml plugin with my Typescript project, but I don't know how to add it in my typings. In @types/nconf, the formats variable is declared as below:

export declare var formats: {
    json: IFormat;
    ini: IFormat;
};

How do I use declaration merging to add yaml, so that it will become:

export declare var formats: {
    json: IFormat;
    ini: IFormat;
    yaml: IFormat
};
like image 626
yoneal Avatar asked May 05 '26 16:05

yoneal


1 Answers

I don't think you can do that. You can't modify the type of this variable because it is inlined! If the interface was declared separately, it would have been possible. So you can submit a pull request to their repo if you want :D

If nconf typings was defined this way :

export interface IFormats {
    json: IFormat;
    ini: IFormat;
}
export declare var formats: IFormats;

You would have created a file index.d.ts containing:

import * as nconf from "nconf";

declare module "nconf" {
     export interface IFormats {
        json: nconf.IFormat;
        ini: nconf.IFormat;
        yaml: nconf.IFormat;
    }
}

It would have solved your problem.

In the meantime you can still cast your variable before using it to ignore the type:

(nconf.formats as any).yaml
like image 127
Sébastien Avatar answered May 07 '26 11:05

Sébastien



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!