【发布时间】:2019-04-16 19:51:14
【问题描述】:
我有一个汉堡菜单,它将接收两个道具:1)isOpen,和 2)一个文件对象 { name, type, size, modifiedAt, downloadUrl }
我正在尝试做这样的事情,但 Typescript 抱怨
const FileManagerSlide = () => {
const [burger, setBurger] = useState({
isOpen: false,
file: {
name: null,
modifiedAt: null,
size: null,
type: null,
downloadUrl: null
}
});
...
<Tr
onClick={() =>
setBurger({
isOpen: true,
file: {
name: "HR STUFF",
modifiedAt: "01/03/2019",
size: 40,
type: "folder",
downloadUrl: "www.google.com"
}
})
}
>
这是错误:
ERROR in [at-loader] ./src/components/file-manager/file-manager-slide.tsx:287:31
TS2345: Argument of type '{ isOpen: true; file: { name: string; modifiedAt: string; size: number; type: string; downloadUrl...' is not assignable to parameter of type 'SetStateAction<{ isOpen: boolean; file: { name: null; modifiedAt: null; size: null; type: null; d...'.
Type '{ isOpen: true; file: { name: string; modifiedAt: string; size: number; type: string; downloadUrl...' is not assignable to type '(prevState: { isOpen: boolean; file: { name: null; modifiedAt: null; size: null; type: null; down...'.
Type '{ isOpen: true; file: { name: string; modifiedAt: string; size: number; type: string; downloadUrl...' provides no match for the signature '(prevState: { isOpen: boolean; file: { name: null; modifiedAt: null; size: null; type: null; downloadUrl: null; }; }): { isOpen: boolean; file: { name: null; modifiedAt: null; size: null; type: null; downloadUrl: null; }; }'.
我想知道我是否定义了File 类型并将汉堡状态的文件属性转换为它可以工作?
interface File {
name: string;
modifiedAt: string;
size: number;
type: string;
downloadUrl: string;
}
但我不知道该怎么做
【问题讨论】:
标签: typescript