Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.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
Typescript 自定义类型脚本挂钩中的可选道具_Typescript - Fatal编程技术网

Typescript 自定义类型脚本挂钩中的可选道具

Typescript 自定义类型脚本挂钩中的可选道具,typescript,Typescript,我试图在自定义钩子上允许可选的道具,如果没有传入的话,这些道具会有一个默认值,但我会不断收到typescript错误 interface IProps { start: number, timeout: 2000, } const useCustomHook = ({start = 10, timeout= 500 }: IProps | null) => { .... }); 我在开始和超时时出错 Property 'start' does not exist on typ

我试图在自定义钩子上允许可选的道具,如果没有传入的话,这些道具会有一个默认值,但我会不断收到typescript错误

interface IProps {
  start: number,
  timeout: 2000,
}

const useCustomHook = ({start = 10, timeout= 500 }: IProps | null) => {
 ....
});
我在开始和超时时出错

Property 'start' does not exist on type 'IProps | null'.
Component.js 我希望能够传递道具,或者让道具为空

const [value] = useCustomHook();

The error i'm getting is Expected 1 arguments, but got 0.

将其初始化为非空值,并使用
Partial
允许选项

固定的
接口IProps{
开始:数字,
超时:数字,
}
const useCustomHook=({start=10,timeout=500}:Partial={})=>{
// ....
日志(启动、超时);
};
Partial做什么?
interface IProps {
  start: number,
  timeout: number,
}

const useCustomHook = ({ start = 10, timeout = 500 }: Partial<IProps> = {}) => {
 // ....
  console.log(start, timeout);
};