gpt4 book ai didi

json - 类型 '{}' 不可分配给类型 'IntrinsicAttributes & IntrinsicClassAttributes

转载 作者:搜寻专家 更新时间:2023-10-30 20:32:29 25 4
gpt4 key购买 nike

我目前正在制作一个简单的 React 应用程序。这是我的 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 来修复它,但仍然出现同样的错误,我做错了什么以及为什么 typescript 会这样。我对此很陌生,通过这个例子,我试图了解 React 是如何协同工作的。

最佳答案

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

在 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>
);
}

关于json - 类型 '{}' 不可分配给类型 'IntrinsicAttributes & IntrinsicClassAttributes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48240449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com