flowtype - 如何处理mixins

时间:2016-12-11 18:23:30

标签: javascript flowtype

我定义了一个界面/类型,我不想在任何想要使用mixin的地方重新定义它。想想facebook的mixins: []财产

示例用例:

const mixin = root => Object.assign(root, {bar: () => 'baz'));

class Foo { 
    constructor () { mixin(this); } 
    test () { console.log(this.bar()) } // <-- this gives an error
}

从我在网上找到的内容来看,这就是我所尝试过的:

interface MyMyxin {
    bar () : string;
}

function mixin <T:Object> (root: T) : T & MyMyxin {
    return Object.assign(root, {bar: () => 'baz'});
}

class Foo {
    constructor () {
        mixin(this);
    }

    test () {
        console.log(this.bar());  //  <-- no luck
    }
}

mixin(Foo.prototype) // <--- this also does not work

但是我无法理解mixin方法为对象添加了额外的属性

0 个答案:

没有答案