简体   繁体   English

尝试在我的 React/TypeScript 组件中引用模拟数据 object,同时还将匹配的类型导入为 Props

[英]Trying to reference a mock data object in my React/TypeScript Component while also importing the matching types as Props

I have a React/TypeScript component I'm building where I import a mock data object to work with.我有一个我正在构建的 React/TypeScript 组件,我在其中导入了一个模拟数据 object 来使用。 Since I'm using TypeScript I also have a separate file with the types for each element of the mock data.由于我使用的是 TypeScript 我还有一个单独的文件,其中包含模拟数据的每个元素的类型。

I then import both the mock data and the type definitions into my main component.然后我将模拟数据和类型定义都导入到我的主要组件中。

I'm still learning TypeScript and the problem I'm running into is that I'm getting errors for my mock data stating that it is declared but never used , and I'm getting a type error for my contactGroups saying Cannot find name 'ContactGroup'.ts(2304) Property 'contactGroups' of exported interface has or is using private name 'ContactGroup'.ts(4033) and I have no idea why.我仍在学习 TypeScript,我遇到的问题是我的模拟数据出现错误,指出它已declared but never used ,并且我的 contactGroups 出现类型错误,说Cannot find name 'ContactGroup'.ts(2304) Property 'contactGroups' of exported interface has or is using private name 'ContactGroup'.ts(4033) ,我不知道为什么。

This is my mock data along with the types and my main component below.这是我的模拟数据以及下面的类型和主要组件。 I've tried a variety of ways to pass everything into my component and I just end up getting similar errors stating that something is declared but never used or doesn't exist on the object type.我尝试了多种方法将所有内容传递到我的组件中,但我最终得到了类似的错误,指出某些内容已声明但从未使用过或在 object 类型上不存在。

Mock Data

export const contacts = {
  count: 1,
  contactGroups: [
    {
      contactGroup: 'Family',
      count: 17,
      contacts: [
        {
          member: 'Uncle'
          lastName: 'BILLIARDS',
          firstName: 'BENJAMIN',
          addresses: [
            {
              addressLine1: '123 FAMILY ST',
              addressLine2: 'APT 1208',
              city: 'ATLANTA',
              state: 'GEORGIA',
              zipCode: '12345',
            },
            {
              addressLine1: '456 WORKING BLVD',
              addressLine2: '',
              city: 'ATLANTA',
              state: 'GEORGIA',
              zipCode: '12345',  
            }
          ]
        }
      ]
    }
  ]
}

TYPE DEFINITIONS

export type Contacts = {
  count: number;
  contactGroups: ContactGroup[];
};

export type ContactGroup = {
  contactGroup: string;
  count: number;
  contacts: Contact[];
};

export type Contact = {
  member: string;
  lastName: string;
  firstname: string;
  addresses: Address[];
};

export type Address = {
  addressLine1: string;
  addressLine2: string;
  city: string;
  state: string;
  zipCode: string;
};

Main Component

import React from 'react';
import { contacts } from '../../mock-data/contacts-mock-data';
import { Contacts } from '../../types/contacts';

type Props = {
  contacts: Contacts[];
  contactGroups: ContactGroup[];
};

export const ContactGroupsSection = (props: Props) => {
  const { contactGroups, contacts } = props;
  console.log(contacts)

  let groups = () => {
    for (let i = 0; i < props.contactGroups.length; i++) {
      console.log(contactGroups[i]);
    }
  };

  return <div>{groups}</div>;
};

I see a few issues.我看到几个问题。

First, you have syntax error in your mock data.首先,您的模拟数据中有语法错误。

    {
      member: 'Uncle' // <- missing comma here
      lastName: 'BILLIARDS',
      firstName: 'BENJAMIN',

Cannot find name 'ContactGroup'.ts(2304)找不到名称“ContactGroup”.ts(2304)

You are using the ContactGroup without importing it.您正在使用ContactGroup而不导入它。 You need to import every type that you explicitly use in your code.您需要导入您在代码中明确使用的每种类型。

import { Contacts, ContactGroup } from '../../types/contacts';

declared but never used声明但从未使用过

I believe that's happening for this line:我相信这条线正在发生这种情况:

import { contacts } from '../../mock-data/contacts-mock-data';

You aren't actually using the imported contacts anywhere, because in your component you do:您实际上并没有在任何地方使用导入的联系人,因为在您的组件中您这样做:

const { contactGroups, contacts } = props;

Which creates a now local variable that shadows the imported one.它创建了一个现在局部变量,它会隐藏导入的变量。 I'm not sure how you intend you use this mock data, but you need to actually use the imported mock data somewhere.我不确定您打算如何使用此模拟数据,但您需要在某处实际使用导入的模拟数据。


Property 'contactGroups' of exported interface has or is using private name 'ContactGroup'.ts(4033)导出接口的属性“contactGroups”已经或正在使用私有名称“ContactGroup”.ts(4033)

I don't know what this one's about, or where you're seeing it exactly, but it might be fixed when you import the type.我不知道这个是关于什么的,或者你在哪里看到它,但是当你导入类型时它可能会被修复。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM