Typescript 自定义类型脚本挂钩中的可选道具
我试图在自定义钩子上允许可选的道具,如果没有传入的话,这些道具会有一个默认值,但我会不断收到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
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);
};