首页 > 解决方案 > 使用泛型类型扩展函数 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 操场中相同示例的链接

标签: typescripttypescript-generics

解决方案


推荐阅读