How to increase underline with Typescript 2.0

I have the following snippet to extend underline with sum function

//underscore.extension.ts    
import * as _ from "underscore"

declare module "underscore" {
    export interface UnderscoreStatic {
        sum(items: number[]): number;
    }
}

_.mixin({
    sum: items => { return _.reduce<number, number>(items, function (s, x) { return s + x; }, 0); }
});

However, the use of _. sum () gives me the "Property" sum "does not exist in the type" UnderscoreStatic ".

Well, will someone tell me the correct way to do this?

+4
source share
2 answers

Looking at this a bit more, you also have a global increase that can solve your problem ( https://www.typescriptlang.org/docs/handbook/declaration-merging.html#global-augmentation )

import * as _ from "underscore"

declare global {
    interface UnderscoreStatic {
        sum(items: number[]): number;
    }
}

_.mixin({
    sum: items => { return _.reduce<number, number>(items, function (s, x) {     return s + x; }, 0); }
});

, / , , .

0

typescript , sum() . ?

, ?

; Underscore , :

import * as _ from 'underscore';

interface UnderscoreExtended extends UnderscoreStatic {
    sum(items: number[]): number;
}

_.mixin({
    sum: items => { return _.reduce<number, number>(items, function (s, x) { return s + x; }, 0); }
});

export { UnderscoreExtended } // as UnderscoreStatic }

export default _ as UnderscoreExtended;

import _ from '<your file containing extended underscore>';

_.isNumber(
    _.sum([1, 2])
);
0

Source: https://habr.com/ru/post/1652150/


All Articles