我目前正在做一个简单的反应应用程序。这是我的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来修复它,但是同样的错误仍然出现,我做错了什么,为什么打字本是这样写的。我对此非常陌生,通过这个例子,我试着去验证和反应是如何一起工作的。
发布于 2019-08-01 15:48:11
我仅通过声明一个完全传递给组件的对象就解决了许多“不能分配输入'IntrinsicAttributes & IntrinsicClassAttributes”类型的错误(微软封闭式发行)。
使用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>
);
}发布于 2020-09-11 02:57:12
您所需要的只是正确声明组件类型,以包括支持类型:
interface IMyProps {
myValue: boolean,
}
const MyComponent: React.FC<IMyProps> = (props: IMyProps) => {
...
}
export default MyComponent;然后您可以将其用作:
import MyComponent from '../MyComponent';
...
return <MyComponent myValue={true} />瞧,打字稿是快乐的。它的好处是,类型记录现在只检查是否传递它们实际存在于道具接口中的参数(可以防止输入等等)。
对于标准组件,它将类似于Swapnill的示例中的内容:
class MyComponent extends React.Component<IMyProps, IMyState>{
constructor(props: IMyProps){}
}
export default MyComponent;发布于 2018-01-13 15:05:34
这里的问题不在于您的tslint设置。请看下面的代码片段:
interface SearchBarProps {
term: string;
optionalArgument?: string;
}
interface SearchBarState{
something: number;
}
class SearchBar extends React.Component<SearchBarProps, SearchBarState> {
constructor(props: SearchBarProps){
super(props);
this.state = {
something: 23
};
}
render() {
const {something} = this.state;
return (
<div>{something}</div>
)
}
}在class SearchBar extends React.Component<SearchBarProps, SearchBarState> {中,SearchBarProps和SearchBarState分别表示组件SearchBar的预期道具类型和状态类型。当您使用类型记录时,必须给出propTypes和stateType。
您可以避免使用关键字any提供类型,但我强烈建议您不要走这条“邪恶”的道路,如果您真的想利用类型抄本。在您的情况下,您似乎没有指定状态类型并使用它,修复它将解决这个问题。
编辑1
在接口SearchBarProps中,optionalArgument成为一个可选的参数,因为我们在它前面添加了一个问号?,所以即使您没有显式地传递optionalArgument,<SearchBar term='some term' />也不会显示任何错误。
希望这能解决你的问题!
https://stackoverflow.com/questions/48240449
复制相似问题