我试图允许自定义钩子上的可选道具,如果不传入默认值,则它们具有默认值,但是我一直在遇到打字稿错误。
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.
答案 0 :(得分:1)
将其初始化为非null值,并使用Partial
允许可选。
interface IProps {
start: number,
timeout: number,
}
const useCustomHook = ({ start = 10, timeout = 500 }: Partial<IProps> = {}) => {
// ....
console.log(start, timeout);
};