如何在Typescript中键入此高级功能?

时间:2018-09-18 00:00:13

标签: typescript typescript-typings

这是棘手的功能:

function getPropertyName(obj, expression) {
    var res = {};
    Object.keys(obj).map(k => { res[k] = () => k; });
    return expression(res)();
}

我尝试键入它失败:

  getPropertyName<T>(obj: T, expression: (o: T) => any): string {
    let res = {};
    Object.keys(obj).map(k => { res[k] = () => k; });
    return expression(res as T)();
  }

1 个答案:

答案 0 :(得分:0)

这不会给您任何错误,但是,您的意图对我而言并不完全清楚:

getPropertyName<T>(obj: T, expression: (o: T) => any): string {
    const res: { [key: string]: any } = {};
    Object.keys(obj).map((k) => { res[k] = () => k; });
    return expression(res as T)();
}