reactjs - 如何返回动态类型取决于函数中的参数类型
问题描述
我的代码看起来像这样
const styles = ({palette, appStyle}: TTheme) => ({
component: { backgroundColor: palette.primary},
text: appStyle.text,
const withStyle = (customStyle) =>
customStyle({
palette: { primary: 'red'},
appStyle: {text: {color: 'red'}},
})
如何定义此函数的类型const withStyle = (customStyle)
谢谢你的帮助
解决方案
您可以在函数的类型签名中使用类型参数,如下所示:
function withStyle<T extends TTheme, U>(styles: (T) => U) {
...
}
这意味着该函数withStyle
接受一个参数,该参数本身就是一个接受某种类型的函数,该类型T
是 的子类TTheme
并返回其他类型的东西U
。
从您的问题中并不清楚类型签名是什么,但关键是您需要使用泛型类型参数。
推荐阅读
- html - 如何修复超出边界的链接(a-tag)动画?
- android - 如何绑定edittext属性android:启用viewmodel数据?
- amazon-cloudformation - 使用模板创建 Athena 工作组
- python - python类初始化引用现有列表的自定义类
- c# - 我需要使用 C# 中的替换功能将 : 替换为 ":" 形成字符串 "AAAA:123346hadhdhajkkd890" 结果如 "AAAA":"123346hadhdhajkkd890"
- c# - 如何从 web api 控制器发送 csv 格式的数据以响应客户端以创建可视化
- javascript - 指定为解释语言的用户空间
- javascript - 用概率绘图
- vue.js - 如何测试返回文档元素的 Vue.js 插件函数?
- matlab - 颜色传递函数的 Matlab 代码给出错误