vue3+ts项目 在使用fake-progress工具库时没有声明文件 如何给这种没有自带类型文件的工具库写类型文件呢?

//自己写的声明文件 要怎么写

interface Option {
    timeConstant: number
}
declare module 'fake-progress' {
    declare class FakeProgress{
        constructor(option: Option)
        start(): void
        end(): void
    }
}

//源文件

const FakeProgress = function (opts) {
    if (!opts) {
        opts = {};
    }

    this.timeConstant = opts.timeConstant || 1000;
    this.progress = 0;
    this._running = false;
    this._intervalFrequency = 100;
    this.autoStart = opts.autoStart || false;
    this.parent = opts.parent;
    this.parentStart = opts.parentStart;
    this.parentEnd = opts.parentEnd;
    if (this.autoStart) {
        this.start();
    }
};

/**
 * Start fakeProgress instance
 * @method
 */

FakeProgress.prototype.start = function () {
    this._time = 0;
    this._intervalId = setInterval(this._onInterval.bind(this), this._intervalFrequency);
};

FakeProgress.prototype._onInterval = function () {
    this._time += this._intervalFrequency;
    this.setProgress(1 - Math.exp(-1 * this._time / this.timeConstant));
};

/**
 * Stop fakeProgress instance and set progress to 1
 * @method
 */

FakeProgress.prototype.end = function () {
    this.stop();
    this.setProgress(1);
};

/**
 * Stop fakeProgress instance
 * @method
 */

FakeProgress.prototype.stop = function () {
    clearInterval(this._intervalId);
    this._intervalId = null;
};

/**
 * Create a sub progress bar under the first progres
 * @method
 * @param {object} options - options of the FakeProgress contructor
 * @param {object} [options.end=1] - the progress in the parent that correspond of 100% of the child
 * @param {object} [options.start=fakeprogress.progress] - the progress in the parent that correspond of 0% of the child
 */

FakeProgress.prototype.createSubProgress = function (opts) {
    const parentStart = opts.start || this.progress;
    const parentEnd = opts.end || 1;
    const options = Object.assign({}, opts, {
        parent: this,
        parentStart: parentStart,
        parentEnd: parentEnd,
        start: null,
        end: null
    });

    const subProgress = new FakeProgress(options);
    return subProgress;
};

/**
 * SetProgress of the fakeProgress instance and updtae the parent
 * @method
 * @param {number} progress - the progress
 */

FakeProgress.prototype.setProgress = function (progress) {
    this.progress = progress;
    if (this.parent) {
        this.parent.setProgress(((this.parentEnd - this.parentStart) * this.progress) + this.parentStart);
    }
};

if (typeof exports === 'object' && typeof module === 'object') {
    module.exports = FakeProgress;
}
阅读 1.7k
1 个回答
interface Options {
    timeConstant: number;
    autoStart?: boolean;
    parent?: FakeProgress;
    parentStart?: number;
    parentEnd?: number;
}

declare module 'fake-progress' {
    export = FakeProgress;

    class FakeProgress {
        constructor(opts: Options);

        progress: number;

        createSubProgress(opts: Options): FakeProgress;

        end(): void;

        setProgress(progress: number): void;

        start(): void;

        stop(): void;
    }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Microsoft
子站问答
访问
宣传栏