类型“{}”不可分配给类型“IntrinsicAttributes & IntrinsicClassAttributes”

新手上路,请多包涵

我目前正在制作一个简单的反应应用程序。这是我的 index.tsx

 import * as React from 'react';
import * as ReactDOM from 'react-dom';
import App from './components/App';
import registerServiceWorker from './registerServiceWorker';

ReactDOM.render(
  <App />,
  document.getElementById('root') as HTMLElement
);
registerServiceWorker();

在这里我有我的 app.tsx

     import * as React from 'react';
import SearchBar from '../containers/price_search_bar';

interface Props {
  term: string;
}

class App extends React.Component<Props> {

  // tslint:disable-next-line:typedef
  constructor(props) {
    super(props);
    this.state = {term: '' };
  }

  render() {
    return (
      <div className="App">
        <div className="App-header">
          <h2>Welcome to React</h2>
        </div>
        <p className="App-intro">
          this is my application.
        </p>
        <div>
            <form>
            <SearchBar term={this.props.term} />
            </form>
        </div>
      </div>
    );
  }
}

export default App;

还有我的搜索栏容器:

     import * as React from 'react';

interface Props {
    term: string;
}

// tslint:disable-next-line:no-any
class SearchBar extends  React.Component<Props> {

    // tslint:disable-next-line:typedef
    constructor(props) {
        super(props);
        this.state = { term: '' };
    }

    public render() {
        return(
            <form>
                <input
                    placeholder="search for base budget"
                    className="form-control"
                    value={this.props.term}
                />
                <span className="input-group-btn" >
                    <button type="submit" className="btn btn-secondary" >
                        Submit
                    </button>
                </span>

            </form>
        );
    }
}

export default SearchBar;

最后我有我的 tsconfig.json

 {
  "compilerOptions": {
    "outDir": "build/dist",
    "module": "esnext",
    "target": "es5",
    "lib": ["es6", "dom"],
    "sourceMap": true,
    "allowJs": true,
    "jsx": "react",
    "moduleResolution": "node",
    "rootDir": "src",
    "forceConsistentCasingInFileNames": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "noImplicitAny": false,
    "strictNullChecks": true,
    "suppressImplicitAnyIndexErrors": true,
    "typeRoots": [
      "node_modules/@types"
    ],
    "noUnusedLocals": true
  },
  "exclude": [
    "node_modules",
    "build",
    "scripts",
    "acceptance-tests",
    "webpack",
    "jest",
    "src/setupTests.ts"
  ]
}

我在错误之后不断收到不同的错误,当我修复一个错误时会出现另一个错误,我不确定我做了什么让它表现得像这样。这是最新的错误:

 ./src/index.tsx
(7,3): error TS2322: Type '{}' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<App> & Readonly<{ children?: ReactNode; }> & Reado...'.
  Type '{}' is not assignable to type 'Readonly<Props>'.
    Property 'term' is missing in type '{}'.

我试图通过修改我的 tsconfig.json 来修复它,但仍然出现相同的错误,我做错了什么以及为什么打字稿会这样。我对此很陌生,通过这个例子,我试图了解反应是如何一起工作的。

原文由 S. N 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 6.6k
2 个回答

我仅通过声明一个完全传递给组件的对象,就解决了很多“ 不可分配给类型 ‘IntrinsicAttributes & IntrinsicClassAttributes ”类型的错误( Microsoft 已关闭问题)。

在 OP 的示例中,不要使用 term={this.props.term} ,而是使用 {...searchBarProps} 使其工作:

 render() {
  const searchBarProps = { // make sure all required component's inputs/Props keys&types match
    term: this.props.term
  }
  return (
    <div className="App">
      ...
      <div>
          <form>
          <SearchBar {...searchBarProps} />
          </form>
      </div>
    </div>
  );
}

原文由 CPHPython 发布,翻译遵循 CC BY-SA 4.0 许可协议

您只需要正确声明组件类型以包含 props 类型:

 interface IMyProps {
    myValue: boolean,
}

const MyComponent: React.FC<IMyProps> = (props: IMyProps) => {
    ...
}

export default MyComponent;

然后你可以将它用作:

 import MyComponent from '../MyComponent';

...

return <MyComponent myValue={true} />

瞧,打字稿很开心。这样做的好处是,typescript 现在只检查传递它们实际存在于 props 接口中的参数(可以防止拼写错误等)。

对于标准组件,它类似于(Swapnill 示例中已有的内容):

 class MyComponent extends React.Component<IMyProps, IMyState>{
    constructor(props: IMyProps){}
}
export default MyComponent;

原文由 Vočko 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进