score:2
Accepted answer
you need to assert that e.target.name
, which is always just string
, is in fact a valid key of inputs.field
.
const eventname = e.target.name as keyof paymentform['field'];
however your update syntax is not right. you are setting top-level properties of inputs and you are using the value of inputs.field[eventname]
as the property name.
you could do a nested update:
setinputs(inputs => ({
...inputs,
field: {
...inputs.field,
[eventname]: eventvalue
}
}));
but why do we need to have this field
property at all? it's the only property in inputs
so why don't we just make it be the entire state.
interface fields {
paymentmethod: string;
amount: string;
receiptcode: string;
paymentdate: string;
paymentsourceid: string;
}
const paymentform = () => {
const [fields, setfields] = usestate<fields>({
paymentmethod: "",
amount: "",
receiptcode: "",
paymentdate: "",
paymentsourceid: "paymentsourceid"
});
const changedhandler = (
e: react.changeevent<htmlinputelement | htmlselectelement>
) => {
setfields((fields) => ({
...fields,
[e.target.name as keyof fields]: e.target.value
}));
};
return <></>;
};
score:1
your eventname can be a union of the field keys. try this.
type eventname = 'paymentmethod' | 'amount' | 'recieptcode' ...etc;
const eventname = e.target.name as eventname; // or
const eventname = e.target.name as keyof paymentform['field'];
Source: stackoverflow.com
Related Query
- how fix Element implicitly has an 'any' type because expression of type 'string' can't be used to index type?
- react typescript error - element implicitly has an any type because expression of type string cant be used to index type {}
- How do I fix the error "Element implicitly has an 'any' type because the expression of type
- TS | Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Record<SecurityMode, boolean>'
- Element implicitly has an 'any' type because expression of type 'any' can't be used to index type
- Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Palette'
- Element implicitly has an 'any' type because expression of type 'string' can't be used to index type React Typescript
- Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'typeof
- Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{...}'
- Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Breakpoints'
- Global window object: Element implicitly has an 'any' type because index expression is not of type 'number'.ts
- TypeScript - Element implicitly has an 'any' type because expression of type '"storyFile"' can't be used to index type '{}'
- Typescript with React > Element implicitly has an 'any' type because expression of type 'string' can't be used to index
- Element implicitly has an 'any' type because expression of type 'any' can't be used to index type 'Item'
- Nested Object -> Element implicitly has an 'any' type because expression of type 'string' can't be used to index type
- Element implicitly has an 'any' type because expression of type 'any' can't be used to index type '{}' - React Anagram
- Element implicitly has an 'any' type because expression of type 'string | number' can't be used to index type
- error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index 'ModuleType'
- Element implicitly has an 'any' type because expression of type 'number' can't be used to index type 'Number'
- Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'
- Element implicitly has an 'any' type because expression of type 'number' can't be used to index type '{}'
- Element implicitly has an 'any' type because expression of type 'any' can't be used to index type '{ 1: Element; 2: Element; 3: Element; }'. TS7053
- Element implicitly has an 'any' type because expression of type 'any' can't be used to index type with createStyles
- Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'BGColors'
- Typescript error in map(): Element implicitly has an 'any' type because expression of type 'string' can't be used to index type
- How to fix Binding element 'children' implicitly has an 'any' type.ts(7031)?
- error TS2602: JSX element implicitly has type 'any' because the global type 'JSX.Element' does not exist
- How to fix Element type is invalid: expected a string (for built-in components)?
- How to fix Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object
- TypeScript Err: "Element implicitly has an 'any' type because expression of type 'any' can't be used to index type
More Query from same tag
- How to group GLTF element and drag and drop in 3js
- Laravel 8 sanctum api "CSRF token mismatch" 419 error with next.js app
- To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config
- Pagination Algorithm with ellipsis
- How do I create a two-column table in React with vertical headers?
- React Bootstrap scroll down to content after accordion is opened
- Gatsby build error #95313, Audio is not defined
- KotlinJS and React, get change value from TextArea
- Fill form based on dynamic data on reactjs
- How to pass a function as props to a component of Material-UI for React?
- Get component's height every time it renders
- React-table hide various columns from button click
- React setState replacing wrong values
- Do props from state overwrite props from parent, using React-Redux?
- React-Three-Fiber (R3F): GLTF loader loading issue. Why can't I see my model?
- How to update progress
- Recharts 100% width is not working in Nextjs / Reactjs. It is not occupying the entire width and also act like a block
- Select more than one radio button that are mapped into different radio groups - React
- Why does TypeScript give me an error inside <GlobalContext.Provider value={...} >?
- iterating on react components
- Set cookie from API response in React
- Onblur is automatically called on OnFocus event in React
- redux-form unable to type values in fields
- React.JS simple component not rendering nested html tag
- React Js - clear Material UI Datepicker
- react routing issues when routes other that in routing config are hit
- Scrolling inside a TextArea Bootstrap using React
- How do I add the Intercom script in my React app?
- How do I enable smooth animation during dynamic addition of components in react native?
- Reactjs deleting item by ID