typescript输入map对象并用空对象实例化

时间:2017-08-08 14:32:41

标签: typescript

template<typename T>
void UseFunctor(T i_functor)
{
    T functor_backup = i_functor; // fails to compile because of copying lambda
    // ...
    functor_backup();
}

int main() 
{
    std::unique_ptr<int> p_int = std::make_unique<int>(10);
    UseFunctor([p_ptr = std::move(p_int)](){});
}

我收到了类型错误

export interface MapObj {
    (s: string): TaskDaylist
}

let map: MapObj = {};

我不能选择吗?

Type '{}' is not assignable to type '(s: string) => TaskDaylist'.
  Type '{}' provides no match for the signature '(s: string): TaskDaylist'.

还有其他方法可以输入地图并使用空对象进行实例化吗?

1 个答案:

答案 0 :(得分:1)

您可以使用type assertion

let map: MapObj = {} as MapObj;

或者简单地说:

let map = {} as MapObj;

code in playground

修改

MapObj的类型是函数的类型,如下所示:

let map: MapObj = function (s: string) {
    return {};
};

如果你只想要一个在字符串(键)和TaskDaylist(值)之间映射的对象,那么它应该如下所示:

interface MapObj {
    [s: string]: TaskDaylist;
}

有关Indexable Types的更多信息。