首页 > 解决方案 > TypeScript 泛型和函数参数条件类型

问题描述

标签: typescripttypescript-generics

解决方案


您错过item了函数签名中的参数,getIdentifier并且看起来它需要 as FuncArg<T>在对象声明中显式。无法更好地解释,对 Typescript 没有太多经验。

const getFoobar2 = <T>(item: T) => {
  if ('id' in item)
    return getFoobar({ item: item } as FuncArg<T>);
  return getFoobar({item: item, getIdentifier: (item: T) => Math.random().toString()} as FuncArg<T>);
}

推荐阅读