ryegrammer
ryegrammer

Reputation: 163

Add an Array to Model and Data in Angular 2 with TypeScript

I get the following error when trying to add a descriptives array to my data and model. error TS2322: Type '{ "id": string; "name": string; "competency": string; "descriptives": string[]; "year": string; }[]' is not assignable to type 'Competency[]'. Type '{ "id": string; "name": string; "competency": string; "descriptives": string[]; "year": string; }' is not assignable to type 'Competency'. Types of property 'descriptives' are incompatible. Type 'string[]' is not assignable to type 'Descriptive[]'. Type 'string' is not assignable to type 'Descriptive'. app/app.routes.ts(9,36): error TS2307: Cannot find module './inventory/inventory.component'.

mocksCompetency.ts

    import { Competency } from './competency';
    export const COMPETENCY: Competency[] = [
{
    "id": "EngLA001",
    "name": "Oral Language",
    "competency": "The teacher understands the importance of oral language, knows the development processes of oral language and provides the students with varied opportunities to develop listening and speaking skills.",
    "descriptives": ["Something","More"],
    "year": "2014",
}
    ];

competency.ts

    export class Competency {
    id: string;
    name: string;
    competency: string;
    descriptives: Descriptive[];
    year: string;
    }
    export class Descriptive {
        description: string;
    }

Upvotes: 0

Views: 2420

Answers (1)

Daniel Tabuenca
Daniel Tabuenca

Reputation: 13661

Your problem is here:

"descriptives" : ["Something More"]

According to your class, descriptives should be of type Descriptive[]. And Descriptive is defined as:

export class Descriptive {
     description: string;
}

This means that you should really be doing:

"descriptives": [{description: "Something More"}]

As an aside, you should probably be using interface instead of class for Competency and Descriptive.

Upvotes: 1

Related Questions