typescript - TypeScript 泛型和函数参数条件类型
问题描述
解决方案
您错过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>);
}
推荐阅读
- azure-active-directory - 使用 MSAL 实现 AAD 身份验证时,我将使用 navigateToLoginRequestUrl 选项做什么?
- php - 我正在寻找一种从电影数据库中获取数据的方法
- html - Html:尝试将导航块与 svg 图像并列
- gremlin - Gremlin 在遍历路径时根据传入边缘过滤掉去边缘
- node.js - 如何使用 MySQL db 部署 Strapi?
- java - 将 .txt 文件传输到数组时出错(线程异常)
- excel - VBA 输入框没有正确显示字符串?
- python - 两个相同的链接在从中解析用户名时表现不同
- javascript - 当光标离开子元素悬停父元素时如何防止父元素的鼠标悬停?
- javascript - 在 onClick 函数中调度三个动作然后使用该响应映射数据是否同步