NodeJS Typescript嵌套对象/ Json

cgh8pdjw  于 2022-11-22  发布在  Node.js
关注(0)|答案(5)|浏览(187)

我是编程和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)

请我需要你的建议,如何解决这个问题或如何创建一个打字界面,将消除这个错误消息。

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

cxfofazt1#

您可以这样做:

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

尝试重构您的界面

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
9q78igpj

9q78igpj2#

你有两个选择。
首先是使用[id:数字]如下所示:

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

第二种方法是将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"],
  }
]
dz6r00yl

dz6r00yl3#

collectionInfo的结构化方式暗示此类型的示例将是包含单个属性id的对象,该属性本身表示包含一些其他属性的对象。
你需要修改你的类型定义,让对象包含一些Map。这两个解决方案是等价的,尽管我更喜欢第二个:
第一个

bq3bfh9z

bq3bfh9z4#

我可以注意到两个问题:
1.接口定义中有尾随管道|。虽然这可能不完全是问题所在。

  1. recordCollection使用1100代替在接口collectionInfo中定义的id
    下面是我认为它应该看起来:
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"],
    },
}

或者,如果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"],
    },
}
ukxgm1gy

ukxgm1gy5#

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

相关问题