简体   繁体   English

打字稿/反应:模块没有导出成员

[英]Typescript/React: Module has no exported member

I am trying to export prop types from one view component to another container component and use them as state type definitions: 我试图将prop类型从一个视图组件导出到另一个容器组件,并将其用作状态类型定义:

// ./Component.tsx
export type Props {
    someProp: string;
}

export const Component = (props: Props ) => {
    ...etc
}

// ./AnotherComponent.tsx
import { Component, Props as ComponentProps } from "./Component";

type Props = {}

type State = ComponentProps & {};

class AnotherComponent extends React.Component<Props, State> {
    state: State;
    ...etc
}

However I am getting in console: 但是我进入控制台:

Module '"./Component "' has no exported member 'Props'.

Any suggestions for how I should be going about this? 关于我应该如何处理的任何建议?

I have also tried using lookup types as per this article, but no joy. 我已经使用查找类型按也试过这样的文章,但没有喜悦。

Try something like this. 尝试这样的事情。 Basically extend the component Props, to include your desired import. 基本上扩展Props组件,以包括所需的导入。

export interface IProps extends PropsOfYouChoiceThatYouImport {
 morePropsHere
}

Also do not include Props into State it is completly wrong IMO. 也不包括国家规定的完全错误的IMO道具。 Do something like this: 做这样的事情:


interface IProps extends YourDesiredProps {
  someProp: string | null;
  // etc
}

interface IState {
  something: boolean
  // etc
}

export class ComponentName extends Component<IProps, IState> {
  // Define you state, and props here. You need to initialize the correct IState 
  // and IProps above
}

As for the console error, I am not sure you can do that.Try create a folder called Types or Interfaces . 至于控制台错误,我不确定您能做到这一点。尝试创建一个名为TypesInterfaces的文件夹。 Then export that one. 然后导出那个。

And import it as named or default export, whatever you wish, at both files. 并根据需要将其导入为命名导出或默认导出,在两个文件中都可以。 Since you want to reuse it. 既然你想重用它。 Tell me if it helps. 告诉我是否有帮助。 I can look more into it, if you make a quick codeSandbox.. 如果您快速编写了一个代码沙箱,我可以对其进行更多研究。

Basically what I suggest is put this into a separate file: 基本上我建议将其放入单独的文件中:

export type Props {
    someProp: string;
}

If you check the React typings ( node_modules/react/index.d.ts ) of React.Component the structure is the following: 如果您检查阵营分型( node_modules/react/index.d.ts的) React.Component的结构如下:

class Component<P, S> {...}

where P equal props and S means state. 其中P等于道具, S表示状态。 You should not mix props and state. 您不应该混合道具和状态。

To fix your TypeScript error change type to interface : 要修复您的TypeScript错误更改type ,请输入interface

export interface Props {
  someProp: string;
}

The issue for the missing member export was the fact that I had an intermediary index file which TypeScript doesn't seem to be able to use when exporting types: 缺少成员导出的问题是我有一个中间索引文件,而TypeScript在导出类型时似乎无法使用:

// ./Component/index.tsx
export { Component, Props } from "./js/Component";

When I change the import to use an exact path to the file rather than trying to import it through the index file it picked it up: 当我更改导入以使用文件的确切路径而不是尝试通过索引文件导入时,它选择了它:

// ./AnotherComponent/js/AnotherComponent.tsx
import { Props as ComponentProps} from "./Component/js/Component";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM