1

I have the following class:

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

Exporting to the following mock data file:

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."
    }];

However, when I try to transpile from typescript I'm getting the following error:

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

How do I correctly implement and reference the data?

2
  • 1
    The error you have is solely a TS typing problem. The types are Array<any> and Object respectively. If you experience runtime problems in specs due to the fact that they are plain objects and not Character instances, see this. Commented Aug 12, 2016 at 16:38
  • I came across this issue when I was running into TS2304 errors, where it said that "object" was not a defined type? I fixed it by replacing 'object' declarations with {} curly braces. Just in case anyone comes across this question with a similar issue. Commented Feb 27, 2017 at 21:05

1 Answer 1

1

I fixed it like this:

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 was helpful

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.