【问题标题】:TypeScript Property 'selected' does not exist on type '{ id: string; value: string; label: string; color: string; }'.ts(2339)TypeScript 属性 'selected' 在类型 '{ id: string; 上不存在值:字符串;标签:字符串;颜色:字符串; }'.ts(2339)
【发布时间】:2021-05-24 16:29:54
【问题描述】:

我正在使用带有 react native 的 TypeScript,当我编写代码时出现错误,即 类型 '{ id: string; 上不存在属性 'selected'值:字符串;标签:字符串;颜色:字符串; }'.ts(2339) 和类型 'string' 不可分配给类型 '{ id: string;值:字符串;标签:字符串;颜色:字符串; } |未定义'.ts(2322)

我的代码是:

export default class Instant extends Component {

  state = {
    one: [

      {
        id: "a0",
        value: "a0",
        label: 'A0',
        color: 'grey',
        checked: false,
      },
      {
        id: "a1",
        value: "a1",
        label: 'A1',
        color: 'grey',
        checked: false,
      },
      {
        id: "a2",
        value: "a2",
        label: 'A2',
        color: 'grey',
        checked: false,
      },
      {
        id: "a3",
        value: "a3",
        label: 'A3',
        color: 'grey',
        checked: false,
      },
      {
        id: "a4",
        value: "a4",
        label: 'A4',
        color: 'grey',
        checked: false,
      },
      {
        id: "a5",
        value: "a5",
        label: 'A5',
        color: 'grey',
        checked: false,
      },
    ],
    data: [
      {
        id: "Normal",
        value: "normal",
        label: 'normal',
        color: 'grey',
      },
      {
        id: "Gloss",
        value: "gloss",
        label: 'gloss',
        color: 'grey',
      },
      {
        id: "Strong",
        value: "strong",
        label: 'strong',
        color: 'grey',
      },
      {
        id: "Re-used",
        value: "reused",
        label: 're-used',
        color: 'grey',
      },
    ],
    two: [
      {
        id: "Color",
        value: "color",
        label: 'color',
        color: 'grey',
      },
      {
        id: "Black&White",
        value: "black&white",
        label: 'black and white',
        color: 'grey',
      },
    ],
    three: [
      {
        id: "Two-side",
        value: "twoside",
        label: 'two side',
        color: 'grey',
      },
      {
        id: "One-side",
        value: "oneside",
        label: 'one side',
        color: 'grey',
      },
    ],
  };

  onPress = (data: any) => this.setState({ data });
  render() {
    let selectedButton = this.state.data.find(s => s.selected == true);
    selectedButton = selectedButton ? selectedButton.value : this.state.data[0].label;

最终的结果一定是这样的,它工作正常但在VSCode上仍然有错误。 enter image description here

【问题讨论】:

  • this.state.data 中的对象确实没有selected 属性。他们应该这样做吗?是可选的吗?
  • 如果您只允许选择其中一个,您可以考虑使用 selected 状态成员来分配对象,而不是使其成为对象的属性。跨度>
  • 这是一个即时计算客户预购的代码,这取决于客户选择什么,所以它是每个选项组中的一个选择

标签: reactjs typescript native


【解决方案1】:

您没有为您的对象定义selected 属性。 在您的对象模型中定义一个 optional 属性。 例如:

export interface stateModel {
  id: string;
  value: string;
  label: string;
  color: string;
  checked: boolean;
  selected?: boolean;
}

或者,如果您无法为您的数据定义模型,您可以使用类型 any 来表示 state

 state : any = {...}

注意:

any 类型允许您将字面意义上的“any”特定值分配给该变量

【讨论】:

  • 我使用第二种方式然后出现错误:在“e”参数中 let selectedButton = this.state.data.find(e => e.selected == true);这是“参数'e'隐式具有'any'类型。”
  • 参数 'e' 隐含一个 'any' 类型
  • 这意味着您正在使用--noImplicitAny。试试这个:this.state.data.find(s: any => s.selected == true)
  • 或者在tsconfig.json中将"noImplicitAny": false添加到"compilerOptions"对象
猜你喜欢
  • 1970-01-01
  • 2022-01-21
  • 2021-12-18
  • 2021-11-27
  • 1970-01-01
  • 2022-09-26
  • 2019-08-25
  • 2020-12-16
  • 2019-11-21
相关资源
最近更新 更多