我目前正在使用 TypeScript 进行 React 项目,但遇到了一个非常愚蠢的问题,而且非常烦人……
例如,我创建了一个虚拟组件Page,它需要一个page类型Page作为props:
interface Props {
page: Page
}
export interface Page {
id: number
category: PageCategory
path: string
name: string
}
const Page: React.FunctionComponent<Props> = (props) => {
...
return (
...
<h1>{ props.page.name }<h1/>
...
export default Page
到目前为止没有问题,但是一旦我决定导入具有以下类型的组件,它们就会出现:
import Page, { Page } from './component/Page' // ts-error: 'Duplicate identifier 'Page''
所以为了避免这个问题,我在I我所有的接口中添加了前缀,IPage但我相信有一种更优雅的方法来做到这一点。你怎么处理?