I added onChange method in my project(React, TS, Mobx), but I am getting an error: Argument of type '{ [x: number]: any; }' is not assignable to parameter of type
I am new to TypeScript and not sure why it's happening. What can be the problem?
(parameter) event: {
target: {
name: any;
value: any;
};
}
Argument of type '{ [x: number]: any; }' is not assignable to parameter of type 'IAddPlayerFormState | ((prevState: Readonly, props: Readonly) => IAddPlayerFormState | Pick | null) | Pick<...> | null'.
import React, { Component } from 'react';
import ViewStore from '../../../store/ViewStore';
import { TextField } from './../../../utils';
interface IAddPlayerFormProps {
viewStore?: ViewStore; // Optional ViewStore
}
interface IAddPlayerFormState {
playerName: string;
isDisabled: boolean;
}
class AddPlayerForm extends Component<IAddPlayerFormProps, IAddPlayerFormState> {
constructor(props: Readonly<IAddPlayerFormProps>) {
super(props);
this.state = {
isDisabled: false,
playerName: '',
};
}
public onChange(event: { target: { name: any; value: any; }; }) {
this.setState({ [event.target.name]: event.target.value });
console.log('On Change!');
}
public handleSubmit = () => {
console.log('On submit!');
}
public render() {
const { isDisabled } = this.state;
return (
<form onSubmit={this.handleSubmit}>
<TextField
type="text"
name="name"
value={name}
placeholder="Add player"
onChange={this.onChange}
disabled={isDisabled}
/>
<input type="submit" />
</form>
);
}
}
export default AddPlayerForm;
The error "Argument of type string | undefined is not assignable to parameter of type string" occurs when a possibly undefined value is passed to a function that expects a string . To solve the error, use a type guard to verify the value is a string before passing it to the function.
The error "Argument of type is not assignable to parameter of type 'never'" occurs when we declare an empty array without explicitly typing it and attempt to add elements to it. To solve the error, explicitly type the empty array, e.g. const arr: string[] = []; . Here are 2 examples of how the error occurs.
The "Type 'string' is not assignable to type" TypeScript error occurs when we try to assign a value of type string to something that expects a different type, e.g. a more specific string literal type or an enum. To solve the error use a const or a type assertion.
Initialize Axios for React Typescript API call example ts file with following code: import axios from "axios"; export default axios. create({ baseURL: "http://localhost:8080/api", headers: { "Content-type": "application/json" } }); You can change the baseURL that depends on REST APIs url that your Server configures.
You need to tell Typescript that your object will have one or more property from IAddPlayerFormState, but not necessarily all properties. You can do it like this:
public onChange(event: { target: { name: any; value: any; }; }) {
const newState = { [name]: value } as Pick<IAddPlayerFormState, keyof IAddPlayerFormState>;
this.setState(newState);
console.log("On Change!");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With