typescript - 使用泛型类型扩展函数 args
问题描述
嗨,下面的例子,我不清楚为什么编译器在传递 foo 的附加参数时没有抱怨,而没有将包含该属性的泛型传递给函数调用。这是预期的行为吗?
理想情况下,我只想在将其他参数作为泛型传递给函数调用时才允许它们,这可能吗?
type Props<ExtraArgs> = {
isOpen: boolean
} & ExtraArgs
const myFunc = <ExtraArgs>(props: Props<ExtraArgs>) => { }
myFunc({ isOpen: true, foo: 'xxx' })
// ☝ Im not passing <ExtraArgs> as a generic containing { foo: string } so why isnt the compiler complaining about the extra arg?
这是TS 操场中相同示例的链接
解决方案
推荐阅读
- datagridview - 将kartik gridview导出到excel时前导零被截断
- jquery - Bootstrap手风琴动态创建不折叠
- c# - ASP.Net Core Web API - ICollection 未显示在 JSON 结果集中
- c - 为什么变量 i 没有在循环中递增?
- junit - Mockito 无法模拟此类:class javax.servlet.jsp.PageContext
- jinja2 - 尝试在 sls 中使用 Jinja - 没有运气
- c# - 错误:“用户 'NT AUTHORITY\ANONYMOUS LOGON' 登录失败”。当第一次部署 asp.net mvc 4 项目时
- java - JAVA如何删除或模式和字符串数组
- javascript - 如何在按钮单击时隐藏和播放音频标签
- linux - 计算来自不同文件的 3 种分隔符(逗号、分号、管道)