打字稿:如何定义可调用对象?

时间:2018-11-28 10:41:00

标签: typescript invoke

这来自@ types / d3:

export interface ScaleContinuousNumeric<Range, Output> {
    (value: number | { valueOf(): number }): Output;

但这是一个接口。如何定义一个类,可以像函数一样调用其实例?像这样:

const inverter = new Inverter();
const question = inverter(42);

也许有某种特殊的ES6符号,例如

class Inverter {
    [Symbol.invokable]() {

    }
}

还是TypeScript语法?

2 个答案:

答案 0 :(得分:1)

您只需定义一个常规函数即可满足带有调用签名的接口:

export interface ScaleContinuousNumeric<Range, Output> {
    (value: number | { valueOf(): number }): Output;
}

let a : ScaleContinuousNumeric<number, string> = (value) => value.toString()

如果接口还有其他方法,则可以使用Object.assign创建函数并添加属性:

export interface ScaleContinuousNumeric<Range, Output> {
    (value: number | { valueOf(): number }): Output;
    otherValue: Range
}

let a: ScaleContinuousNumeric<number, string> = Object.assign(
    (value: number | { valueOf(): number }) => value.toString(), {
        otherValue: 1
    });

或者在打字稿3.0中,您可以创建函数,分配额外的属性,然后该函数将与接口兼容:

export interface ScaleContinuousNumeric<Range, Output> {
    (value: number | { valueOf(): number }): Output;
    otherValue: Range
}

function myFunc(value: number | { valueOf(): number }) {
    return value.toString()
}
myFunc.otherValue = 1
let a: ScaleContinuousNumeric<number, string> = myFunc

答案 1 :(得分:0)

我有类似的问题。我需要在Typescript中实现自定义比例。以下工作。

let scaleLinear = function () {

    // To translate from domain [a, b] to range [c, d] we use:
    // y = x.m + n;
    // where
    // m = (c - d)/(a - b)
    // n = (a.d - b.c)/(a - b)
    //
    let _range = [0, 1];
    let _domain = [0, 1];
    let _clamp = false;
    let _m = 1.0;
    let _n = 0.0;

    function scale(value: number) {
        return value * _m + _n;
    }

    scale.rescale = function () {
        // Called internally to recalc scale values
        _m = (_range[0] - _range[1]) / (_domain[0] - _domain[1]);
        _n = (_domain[0] * _range[1] - _domain[1] * _range[0]) / (_domain[0] - _domain[1]);
    }

    scale.invert = function (value:number) {
        return (value - _n) / _m;
    }

    scale.domain = function (domain?: any): any {
        if (arguments.length) {
            _domain = arguments[0].slice();
            scale.rescale();
            return scale;
        }
        return _domain;
    }

    scale.range = function (range?: any): any {
        if (arguments.length) {
            _range = arguments[0].slice();
            scale.rescale();
            return scale;
        }
        return _range;
    }

    // Statics
    scale.nice = function () {
        // do nothing
    }

    return scale;

};