Reactjs 如何强制typescript检查解构道具?
当我使用解构时,Typescript不会检查道具 我已经尝试过typescript的严格标志 让我们描述一下简单的组件Reactjs 如何强制typescript检查解构道具?,reactjs,typescript,Reactjs,Typescript,当我使用解构时,Typescript不会检查道具 我已经尝试过typescript的严格标志 让我们描述一下简单的组件 const-Component=(props:{foo:string})=>; 下一个代码将抛出typescript错误 但这不是 那么,如何强制typescript在两种代码变体上显示错误呢?接口IProps{ interface IProps { name: string; } const Test = <T extends IProps>(p
const-Component=(props:{foo:string})=>;
下一个代码将抛出typescript错误
但这不是
那么,如何强制typescript在两种代码变体上显示错误呢?接口IProps{
interface IProps {
name: string;
}
const Test = <T extends IProps>(props: T & Record<Exclude<keyof T, keyof IProps>, "Only props of Test">) => <div>HELLO</div>
const FakeProps = {name: "hello", age: 24};
const Test1 = <Test {...FakeProps}/> // failure because of age
名称:字符串;
}
常量测试=(道具:T&Record)=>HELLO
const FakeProps={姓名:“你好”,年龄:24};
const Test1=//由于年龄原因而失败
希望这有帮助,这会解决你的问题这是一个常见的伎俩,迫使多余的财产检查