如何扩展内置函数/向函数添加属性?

时间:2018-08-09 17:33:06

标签: node.js typescript

process.hrtime.bigint()在标准库中不存在。我正在尝试将其添加。

这就是我所拥有的:

types / bigint.d.ts

// https://github.com/Microsoft/TypeScript/issues/15096#issuecomment-400186862
type BigInt = number
declare const BigInt: typeof Number;

declare namespace NodeJS {
    export interface Process {
        hrtime: HighResTime
    }

    export interface HighResTime {
        (time?: [number, number]): [number, number];
        bigint(): BigInt
    }
}

我在tsconfig.json中的typeRoots中添加了以下内容:

"typeRoots": ["./node_modules/@types","./types"]

使用方式:

///<reference path="../types/bigint.d.ts"/>

export default class ProgressBar {
    private startTime?: BigInt;

    start() {
        this.startTime = process.hrtime.bigint();
        this.render();
    }

但是我得到了

/home/me/Projects/xxx/node_modules/ts-node/src/index.ts:261
    return new TSError(diagnosticText, diagnosticCodes)
           ^
TSError: ⨯ Unable to compile TypeScript:
src/ProgressBar.ts(19,41): error TS2339: Property 'bigint' does not exist on type '(time?: [number, number] | undefined) => [number, number]'.

如果我将<reference>删除为bigint.d.ts,则情况变得更糟,并抱怨类型BigInt不存在(TS2304)–这对我,我以为那是typeRoot的重点?

但是更大的问题是,即使有了参考,TS仍然不喜欢process.hrtime.bigint(),所以我猜测我没有适当地扩展Process接口。我该怎么办?

0 个答案:

没有答案