我是打字稿新手,并且创建了这个对象:
const initialState = {
title: {
value: "",
error: false
},
amount: {
value: 0,
error: false
},
type: "Food",
time: new Date()
}
我要使用的是useState的初始状态。因此,我现在想将需要作为类型传递给useState的信息。
const [Form, setForm] = useState< "what should come here?" >(initialState)
;
预先感谢
答案 0 :(得分:2)
interface IState {
title: {
value: string;
error: boolean;
};
amount: {
value: number;
error: boolean;
};
type: string;
time: Date;
}
const [Form, setForm] = useState<IState>(initialState);
答案 1 :(得分:1)
您可以执行以下操作:
interface SomeName {
value: string;
error: boolean;
}
interface IState {
title: SomeName;
amount: SomeName;
type: string;
time: Date;
}
const initialState: IState = {
title: {
value: "",
error: false
},
amount: {
value: 0,
error: false
},
type: "Food",
time: new Date()
}
const [Form, setForm] = useState<IState>(initialState);