简体   繁体   English

Typescript 嵌套对象 / Json

[英]Typescript Nested Objects / Json

I'm new to programming and JavaScript, i tried creating an interface for a code challenge, but i keep getting this error message:我是编程新手,JavaScript,我尝试为代码挑战创建一个界面,但我不断收到此错误消息:

Type '{ 1100: { albumTitle: string; artist: string; tracks: string[]; }; 2468: { albumTitle: string; artist: string; tracks: string[]; }; 1245: { artist: string; tracks: never[]; }; 5439: { albumTitle: string; }; }' is not assignable to type 'collectionInfo'.
  Object literal may only specify known properties, and '1100' does not exist in type 'collectionInfo'.ts(2322)

Please i need your suggestions on how to resolve this or how to create a typescript interface that will eliminate this error message.请就如何解决此问题或如何创建 typescript 接口来消除此错误消息提出建议。


This my typescript interface attempt:

interface collectionInfo {
        id : {
            albumTitle: string | number |;
            artist: string | number |;
            tracks: string[] | number[] | null; 
        }
}

const recordCollection: collectionInfo = {
    1100: {
      albumTitle: "Prisoner",
      artist: "Lucky Dube",
      tracks: ["Prisoner", "Don\'t Cry"],
    },

You can do it like this:你可以这样做:

interface CollectionInfo {
   [id: number]: {
      albumTitle: string | number;
      artist: string | number;
      tracks: string[] | number[] | null; 
   }
}

try refactoring your interface尝试重构你的界面

interface CollectionInfo {
   id: number;
   albumTitle: string; // I recommend sticking to one type only
   artist: string | number;
   tracks?: string[] | number[]; // if you are trying to add optional property, use ? to make it optional
}

const recordCollection: CollectionInfo = {
   id: 1100,
   albumTitle: "Prisoner",
   artist: "Lucky Dube",
   tracks: ["Prisoner", "Don't Cry"]
}

// Usage
console.log(recordCollection.id); // 1100
console.log(recordCollection.albumTitle); // Prisoner

You have 2 options.你有两个选择。

First is to use [id: number] like this:首先是像这样使用 [id: number] :

interface collectionInfo {
  [id: number] : {
    albumTitle: string | number | null;
    artist: string | number | null;
    tracks: string[] | number[] | null; 
  }
}

const recordCollection: collectionInfo = {
  1100: {
    albumTitle: "Prisoner",
    artist: "Lucky Dube",
    tracks: ["Prisoner", "Don\'t Cry"],
  }
}

And the second way is to add id to properties and then use an array instead like this:第二种方法是将 id 添加到属性,然后使用数组代替,如下所示:

interface collectionInfo {
  id: number;
  albumTitle: string | number | null;
  artist: string | number | null;
  tracks: string[] | number[] | null;
}

const recordCollection: collectionInfo[] = [
  {
    id: 1100,
    albumTitle: "Prisoner",
    artist: "Lucky Dube",
    tracks: ["Prisoner", "Don\'t Cry"],
  }
]

collectionInfo is structured in a way that implies that an instance of this type would be an object containing a single attribute, id , that itself represents an object contains some other attributes. collectionInfo的构造方式暗示此类型的实例将是包含单个属性id的 object,它本身表示包含一些其他属性的 object。

You need to modify your type definition to allow the object to contain somewhat of a map. These two solutions are equivalent, though I would prefer the second one:您需要修改您的类型定义以允许 object 包含一些 map。这两个解决方案是等效的,但我更喜欢第二个:

interface CollectionsInfo {
  [id: number]: {
    albumTitle: string | number;
    artist: string | number;
    tracks: string[] | number[] | null; 
  }
}
interface CollectionInfo {
  albumTitle: string | number;
  artist: string | number;
  tracks: string[] | number[] | null;
}

type CollectionsInfo = Record<number, CollectionInfo>;

There are two issues I can notice:我可以注意到两个问题:

  1. there are trailing pipes |有尾管| in the interface definition.在接口定义中。 Although this might not exactly be the problem.尽管这可能不完全是问题所在。
  2. The recordCollection is using 1100 in the place of id which is defined in the interface collectionInfo . recordCollection使用1100代替接口collectionInfo中定义的id

Below is how I think it should look:以下是我认为它应该看起来的样子:

 interface collectionInfo { id: { albumTitle: string | number; artist: string | number; tracks: string[] | number[] | null; } } const recordCollection: collectionInfo = { id: { albumTitle: "Prisoner", artist: "Lucky Dube", tracks: ["Prisoner", "Don\'t Cry"], }, }

Alternatively, if the id must be a number, then you could try this:或者,如果 id 必须是一个数字,那么你可以试试这个:

 interface collectionInfo { [key: number]: { albumTitle: string | number; artist: string | number; tracks: string[] | number[] | null; } } const recordCollection: collectionInfo = { 1100: { albumTitle: "Prisoner", artist: "Lucky Dube", tracks: ["Prisoner", "Don\'t Cry"], }, }

interface collectionInfo {
        [id: number] : {
            albumTitle: string | number ;
            artist: string | number ;
            tracks: string[] | number[] | null; 
        }
     }

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

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