Arrays 在Angular2中为模拟数据使用数组和对象

Arrays 在Angular2中为模拟数据使用数组和对象,arrays,object,angular,typescript,mocking,Arrays,Object,Angular,Typescript,Mocking,我有以下课程: export class Character { id: number; name: string; portrait: string; abilities: array; equipment: array; statistics: object; money: number; description: string; } 导出到以下模拟数据文件: import {

我有以下课程:

export class Character {
       id: number;
       name: string;
       portrait: string;
       abilities: array;
       equipment: array;
       statistics: object;
       money: number;
       description: string;
}
导出到以下模拟数据文件:

import { Character } from './character';

export const CHARACTERS: Character[] = [{
      "id": 1,
      "name": "Dragon-Bear",
      "portrait": "dbear.png",
      "abilities": ["Wounded Roar", "Summon Wildings", "Bear Sprint", "Alternative Appearance"],
      "equipment": ["Imbedded Thorn", "Bloodstone Necklace", "Spirit Flask"],
      "statistics": { "Business Sense": 47, "Sexuality": 87, "Weirdness": 86, "Persuation": 92, "Cuddliness": 76, "Meme Influence Zone": 23, "Close Reading": 63, "Humanity": 30 },
      "money": 134,
      "description": "Dragon-Bears are wild and ferocious creatures, but also have a gentle, nurturing side. Once a Dragon-Bear mates, it will protect its partner and any offspring with its life. Dragon-Bears also like to cuddle."
    }, {
      "id": 2,
      "name": "Lene-Cow",
      "portrait": "lcow.png",
      "abilities": ["Fade Into Background", "Alter Vibrations", "Cunning Innocence", "Create Milk"],
      "equipment": ["Long Glass Of Milk", "Scarf Of Influence"],
      "statistics": { "Business Sense": 34, "Sexuality": 73, "Weirdness": 92, "Persuation": 74, "Cuddliness": 86, "Meme Influence Zone": 32, "Close Reading": 43, "Humanity": 77 },
      "money": 324,
      "description": "Lene-Cows are canny and stealthy, and beguiled almost everyone they meet. They are, however, occasionally prone to minor fits of rage, but their natural charm and magnetism means people usually accept this trait as a lovable quirk, rather than a character flaw. Lene-Cows are very good at keeping secrets."
    }, {
      "id": 3,
      "name": "Clown-Fox",
      "portrait": "cfox.png",
      "abilities": ["Fox Charm", "Hypnotise"],
      "equipment": ["Judge's Wig", "Clown Nose"],
      "statistics": { "Business Sense": 89, "Sexuality": 98, "Weirdness": 45, "Persuation": 98, "Cuddliness": 21, "Meme Influence Zone": 52,  "Close Reading": 63, "Humanity": 30 },
      "money": 234,
      "description": "Charming and dashing, Clown-Foxes are particularly adept at wooing members of the opposite sex, of whatever species. They are also extremely skilled at convincing people to do business with them."
    }];
但是,当我尝试从typescript进行传输时,出现以下错误:

app/characters/character.ts(5,19): error TS2304: Cannot find name 'array'.
app/characters/character.ts(6,19): error TS2304: Cannot find name 'array'.
app/characters/character.ts(7,20): error TS2304: Cannot find name 'object'.
app/characters/characters-mocks.ts(29,7): error TS2322: Type '({ "id": number; "name": string; "portrait": string; "abilities": string[]; "equipment": string[]...' is not assignable to type 'Character[]'.
  Type '{ "id": number; "name": string; "portrait": string; "abilities": string[]; "equipment": string[];...' is not assignable to type 'Character'.
    Type '{ "id": number; "name": string; "portrait": string; "abilities": string[]; "equipment": string[];...' is not assignable to type 'Character'.
      Object literal may only specify known properties, and '"Description"' does not exist in type 'Character'.
如何正确实现和引用数据?

我这样修复了它:

export class Character {
       id: number;
       name: string;
       portrait: string;
       abilities: string[];
       equipment: string[];
       statistics: {};
       money: number;
       description: string;
}

有帮助

您的错误仅仅是TS键入问题。类型是
Array。我在遇到TS2304错误时遇到了这个问题,它说“object”不是定义的类型?我通过用{}大括号替换“object”声明修复了它。以防万一有人遇到这个问题和类似的问题。