2016-08-12 6 views
1

で模擬データの配列やオブジェクトを使用して、私は次のクラスがあります:私はtypescriptですが、私からtranspileしようとすると、しかしAngular2

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

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

には、次のモックのデータファイルへのエクスポートを次のエラーが発生しました:

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

役に立ちました。型はそれぞれ 'Array 'と 'Object'です。文字インスタンスではなくプレーンオブジェクトであるため、仕様で実行時の問題が発生する場合は、[this](https://stackoverflow.com/questions/38687796/test-broke-when-changed-model-with-インタフェースからクラスへ)。 – estus

+0

ありがとうございました。私は情報を今見てみましょう。私はtypescriptに新しいので、私はそれが私の誤りであると確信しています。 –

+0

私はTS2304エラーが発生したときにこの問題に遭遇しました。ここでは、「オブジェクト」は定義されたタイプではないと言われました。私は、 'object'宣言を{}中括弧で置き換えることで修正しました。ちょうど誰かが同様の問題でこの質問に出くわす場合に備えて。 –

答えて

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あなたが持っているエラーは、単にTSタイピング問題である