【问题标题】:Property 'length' does not exist on type error on importing array of objects导入对象数组时出现类型错误,属性“长度”不存在
【发布时间】:2022-07-01 21:43:17
【问题描述】:

我正在导入这个数组:

    const quotes = [
  { quote: "Design is intelligence made visible.", name: "Alina Wheeler" },
  {
    quote:
      "The public is more familiar with bad design than good design. It is, in effect, conditioned to prefer bad design, because that is what it lives with. The new becomes threatening, the old reassuring.",
    name: "Paul Rand",
  },
  {
    quote: "Every great design begins with an even better story.",
    name: "Lorinda Mamo",
  }
];

进入另一个 .tsx 文件并以这种方式使用它:

import * as quotes from "./quotes/quotes";

const quoteLength = quotes.length - 1;

但我收到此错误:“typeof import”类型上不存在属性“length”

知道如何解决这个问题吗?如何将该对象数组声明为类型?

【问题讨论】:

    标签: reactjs typescript


    【解决方案1】:
    1. 您是否从引号文件中导出了数组?
    2. 如果你有那么 使用import {quotes} from "./quotes/quotes"; 调用它

    【讨论】:

      【解决方案2】:

      你必须这样导出和导入文件,你可以看到下面的代码

      export const quotes = [
        {
          quote: 'Design is intelligence made visible.', 
          name: 'Alina Wheeler'
        },
        {
          quote:
            'The public is more familiar with bad design than good design. It is, in effect, conditioned to prefer bad design, because that is what it lives with. The new becomes threatening, the old reassuring.',
          name: 'Paul Rand'
        },
        {
          quote: 'Every great design begins with an even better story.',
          name: 'Lorinda Mamo'
        }
      ];
      

      .tsx 文件

      import {quotes} from "./quotes/quotes";
      
      const quoteLength = quotes.length - 1;
      

      【讨论】:

        猜你喜欢
        • 2021-10-26
        • 2021-06-19
        • 2020-10-18
        • 1970-01-01
        • 2020-07-07
        • 1970-01-01
        • 1970-01-01
        • 2020-08-07
        • 2018-10-21
        相关资源
        最近更新 更多