推断同级函数的返回类型

时间:2018-10-03 12:22:39

标签: typescript generics typescript-generics

我希望能够做到这一点:

  const columns: ColumnDefinition<Pair>[] = [
    { label: 'Pair', value: pair => (all ? pair.code : pair.second.code), format: result => result.toUpperCase() },
    { label: 'Price', numeric: true, value: pair => 0 },
    { label: 'Change', numeric: true, value: pair => 0 },
  ];

并让TS知道pair的{​​{1}}参数的类型为value,而Pair的{​​{1}}参数的类型为{{1} }(resultformat)。 string返回的结果将是pair.code的结果。 string的返回类型在不同的列定义中可能有所不同。

到目前为止,这是我的类型:

value

但是我收到一条错误消息:

  

通用类型“ ColumnDefinition”需要2个类型参数。

1 个答案:

答案 0 :(得分:2)

extends只是类型定义的一部分。如果要选择忽略通用类型,则需要使用=设置默认值:

export type ColumnDefinition<T, F extends (item: T) => unknown = (item: T) => unknown> = {