Home > other >  Property 'length' does not exist on type error on importing array of objects
Property 'length' does not exist on type error on importing array of objects

Time:07-02

I am importing this array:

    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",
  }
];

into another .tsx file and using it this way:

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

const quoteLength = quotes.length - 1;

But I'm getting this error: Property 'length' does not exist on type 'typeof import

Any idea how I can fix this? How do I declare that array of objects as a type?

CodePudding user response:

  1. Have you exported the array from the quotes file?
  2. If you have then call it using import {quotes} from "./quotes/quotes";

CodePudding user response:

You have to export and import file this way, as you can see the below code

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 file

import {quotes} from "./quotes/quotes";

const quoteLength = quotes.length - 1;
  • Related