ciz*_*z30 3 typescript reactjs
我有 React 和 TypeScript 项目。这是我在某些组件中的文件夹结构,例如:
- ComponentFolder
- index.tsx
- types.d.ts
- ChildComponent
- index.tsx
Run Code Online (Sandbox Code Playgroud)
我知道,如果我将类型放入组件目录根目录中的types.d.ts文件中,我可以在此目录中的 childComponents 中使用这些类型。
所以我尝试了;
我的types.d.ts文件:
export type CommonProps = {
openItem: string;
getValue: (e: React.MouseEvent) => void;
};
Run Code Online (Sandbox Code Playgroud)
并在子组件中:
import * as React from "react";
const ChildComponent: React.FC<CommonProps> = (props) => {
return (
<div>
{props.openItem}
</div>
);
};
export default ChildComponent;
Run Code Online (Sandbox Code Playgroud)
但出现错误:
cannot find CommonProps error.
Run Code Online (Sandbox Code Playgroud)
我如何知道目录中是否有一些文件 d.ts 我可以在该目录中使用这些类型而无需导入。
我哪里弄错了?
删除export
关键字。
type CommonProps = {
openItem: string;
getValue: (e: React.MouseEvent) => void;
};
Run Code Online (Sandbox Code Playgroud)