简体   繁体   English

如何修复 Element 隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型?

[英]how fix Element implicitly has an 'any' type because expression of type 'string' can't be used to index type?

Trying out TypeScript for a React project and I'm stuck on this error.为 React 项目尝试 TypeScript 并且我遇到了这个错误。

TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ paymentMethod: string; TS7053:元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型“{ paymentMethod: string; amount: string;金额:字符串; receiptCode: string;收据代码:字符串; paymentDate: string;付款日期:字符串; paymentSourceID: string;支付源ID:字符串; }'. }'。 No index signature with a parameter of type 'string' was found on type '{ paymentMethod: string;在“{ paymentMethod: string; 类型”上找不到带有“string”类型参数的索引签名amount: string;金额:字符串; receiptCode: string;收据代码:字符串; paymentDate: string;付款日期:字符串; paymentSourceID: string;支付源ID:字符串; }' }'

when i try setInputs()当我尝试 setInputs()

setInputs(inputs => ({...inputs, [inputs.field[eventName]]: eventValue})); setInputs(inputs => ({...inputs, [inputs.field[eventName]]: eventValue}));

and this is my component.这是我的组件。

 import React, {useState} from 'react'; import {DatePicker} from "jalali-react-datepicker"; import axios from "axios"; import PaymentFormClasses from './PaymentForm.module.css'; import PaymentCard from "../../Cards/PaymentCard/PaymentCard"; import Card from "../../Cards/Card"; interface PaymentForm { field: { paymentMethod: string, amount: string, receiptCode: string, paymentDate: string, paymentSourceID: string } } const PaymentForm = () => { const [inputs, setInputs] = useState<PaymentForm>({ field: { paymentMethod: '', amount: '', receiptCode: '', paymentDate: '', paymentSourceID: 'paymentSourceID' } }); const changedHandler = (e: React.ChangeEvent<HTMLInputElement> | React.ChangeEvent<HTMLSelectElement>) => { console.log(typeof e.target.name); const eventName = e.target.name; const eventValue = e.target.value; setInputs(inputs => ({...inputs, [inputs.field[eventName]]: eventValue})); console.log(e.target.value); } return ( <> </> ); }; export default PaymentForm;

You need to assert that e.target.name , which is always just string , is in fact a valid key of inputs.field .您需要断言e.target.name始终只是string ,实际上是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.您正在设置输入的顶级属性,并且正在使用inputs.field[eventName]作为属性名称。

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?但是为什么我们需要这个field属性呢? It's the only property in inputs so why don't we just make it be the entire state.它是inputs中唯一的属性,所以我们为什么不让它成为整个 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 <></>;
};

Your eventName can be a union of the field keys.您的 eventName 可以是字段键的联合。 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'];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型“BGColors” - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'BGColors' 元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型“{}” - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}' 元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型 - Phaser - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type - Phaser 元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型“{ AT: number; BE:数字,...}` - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ AT: number; BE: number,…}` 元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型 A - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type A 元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型样式 - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type style TypeScript - 元素隐式具有“任意”类型,因为“字符串”类型的表达式不能用于索引类型 - TypeScript - 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 '{}' 元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型 React Typescript - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type React Typescript TypeScript - ts(7053):元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引 - TypeScript - ts(7053) : Element implicitly has an 'any' type because expression of type 'string' can't be used to index
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM