【发布时间】:2019-09-06 08:22:44
【问题描述】:
我正在尝试为body、header 和footer 编写类型,但找不到访问它的方法。通常我在对象被解构时会遇到这类问题。
来自body、header 和footer 我收到错误消息:
“常量体:任何。
类型“{}”上不存在属性“body”。”
interface ModalProps {
onClick: () => void;
}
const Modal: React.FunctionComponent<ModalProps> = props => {
useContext(ModalContext);
const [isOpen, setIsOpen] = useState(false);
const [content = {}, setContent] = useState({});
const show = (content = {}) => {
const { body, header, footer } = content;
if (!body && !header && !footer) {
setContent({ content });
} else {
setContent({ body, header, footer });
}
setIsOpen(true);
};
我尝试将其插入界面但没有成功:
interface ModalProps {
onClick: () => void;
content: {
body: any
}
}
也试过了:
const { body, header, footer }: {body: any, header: any, footer: any} = content;
但收到错误消息:“类型 '{}' 缺少来自类型 '{ body: any; header: any; footer: any; }': body, header, footer 的以下属性
任何提示将不胜感激。谢谢!
【问题讨论】:
标签: typescript typescript-typings