Mark
Mark

Reputation: 217

Angular4 and TypeScript property does not exist on type any[]

I can't seem to figure this thing out... I'm getting data from an api and storing it as any[]. I'm trying to get data like this...

this.data.results[i].datas[j].dataType

But I get the error property results does not exist on type any[]. I've tried looking at many other questions and answers but I can't seem to figure it out.

(Should I just create a data object and instead of assigning the data to any[], assign it to the object?) Thanks for your help!

Upvotes: 1

Views: 1636

Answers (2)

FabianTe
FabianTe

Reputation: 649

I'd always (when possible) at least declare an interface like this:

interface IDataset {
    dataType: string;
    value: string;
    // Whatevery your dataset holds
}

interface IResultset {
    datas: IDataset[9]
}

interface IData {
    results: IResultset[];
}

class SomeClass {
    data: IData[];

    someFunction() {
        this.data.results[i].datas[j].dataType = "whatever";
    }
}

That way you get IntelliSense for your data, have to structure your data in a way that makes sense and could use these interfaces on both client and server side (if you are working on the server too)

Upvotes: 1

Poul Kruijt
Poul Kruijt

Reputation: 71961

You should change the type definition of this.data to:

data: {results?: any[]} = {};

Upvotes: 1

Related Questions