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

时间:2016-08-12 16:11:59

标签: 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 { 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."
    }];

然而,当我尝试从打字稿中转换时,我收到以下错误:

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'.

如何正确实施和引用数据?

1 个答案:

答案 0 :(得分:1)

我这样修好了:

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

https://www.typescriptlang.org/docs/handbook/basic-types.html很有帮助