Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何强制typescript检查解构道具?_Reactjs_Typescript - Fatal编程技术网

Reactjs 如何强制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

当我使用解构时,Typescript不会检查道具

我已经尝试过typescript的严格标志

让我们描述一下简单的组件

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=//由于年龄原因而失败
希望这有帮助,这会解决你的问题这是一个常见的伎俩,迫使多余的财产检查