Reactjs typescript上有多个错误
我在asp.net核心应用程序中创建了以下类:Reactjs typescript上有多个错误,reactjs,typescript,Reactjs,Typescript,我在asp.net核心应用程序中创建了以下类: import * as React from 'react'; interface MyInputProps { inputType: string; id: string; className: string; parentFunction: (string) => void; } export class LoginInput extend
import * as React from 'react';
interface MyInputProps {
inputType: string;
id: string;
className: string;
parentFunction: (string) => void;
}
export class LoginInput extends React.Component<MyInputProps, {}> {
constructor() {
super();
this.onChange = this.onChange.bind(this);
}
private onChange(e) {
this.props.parentFunction(e.target.value);
}
public render() {
return <input type={this.props.inputType} id={this.props.id} className={this.props.className} placeholder="username" onChange={this.onChange} />;
}
}
import*as React from'React';
接口MyInputProps{
输入类型:字符串;
id:字符串;
类名:string;
parentFunction:(字符串)=>void;
}
导出类LoginInput扩展React.Component{
构造函数(){
超级();
this.onChange=this.onChange.bind(this);
}
私人更改(e){
this.props.parentFunction(如target.value);
}
公共渲染(){
返回;
}
}
现在我得到以下错误:
- (TS)参数“string”隐式具有“any”类型
- (TS)参数“e”隐式具有“any”类型
import * as React from 'react';
import axios from 'axios';
import Country from './Country';
import CountryModel from './models/CountryModel';
const countriesUri = 'https://restcountries.eu/rest/v2/all?fields=name;alpha2Code';
interface Props {
onChange: (string) => void;
}
interface State {
countries: CountryModel[];
}
class CountryList extends React.Component<Props, State> {
constructor() {
super();
this.state = {
countries: []
};
this.onChange = this.onChange.bind(this);
}
componentWillMount() {
this.getCountries();
}
private getCountries() {
//
}
private onChange(e) {
this.props.onChange(e.target.value);
}
public render() {
return <select onChange={this.onChange} ref='countries'>
{
//
}
</select>;
}
}
export default CountryList;
import*as React from'React';
从“axios”导入axios;
从“/国家”进口的国家;
从“./models/CountryModel”导入CountryModel;
康斯特郡苏里酒店https://restcountries.eu/rest/v2/all?fields=name;字母代码';
界面道具{
onChange:(字符串)=>void;
}
界面状态{
国家:CountryModel[];
}
类CountryList扩展了React.Component{
构造函数(){
超级();
此.state={
国家:[]
};
this.onChange=this.onChange.bind(this);
}
组件willmount(){
这个。getCountries();
}
私营部门(国家){
//
}
私人更改(e){
this.props.onChange(即target.value);
}
公共渲染(){
返回
{
//
}
;
}
}
导出默认国家列表;
您必须定义变量的类型,例如
interface MyInputProps {
parentFunction: (string:string) => void;
}
private onChange(e:any) {
this.props.parentFunction(e.target.value);
}
错误消息基本上意味着,如果不定义类型,它将向其中隐式添加一个
:any
。检查的原因是tsconfig.json
中的编译器选项noImplicitAny:true
,我的问题是,在另一个解决方案中,我得到了:interface Props{onChange:(string)=>void;},它工作正常。也许是我的配置有问题?@V.Vouvonikos是的,正如我所说。您必须向其添加类型,例如string:string
-variableName:variableType
。