I have just started with Angular2 and I\'ve got an issue I cannot really understand.
I have some mock data created as such:
export const WORKFLOW_DAT
When you tell typescript:
WORKFLOW_DATA: Object
You are telling it that WORKFLOW_DATA
is a plain object with no attributes. When you later try to access WORKFLOW_DATA.testDataArray
the compiler thinks you misusing the type.
If you want type checking on WORKFLOW_DATA
you need to create an interface that describes your object.
The return type if your method is Observable<Object>
. So when you subscribe, that will be the type passed. And there is no testDataArray
on the Object
type. You can do a couple things:
Type the data and return type differently
WORKFLOW_DATA: { testDataArray: any } = []
getWorkflowForEditor(): Observable<{ testDataArray: any }>
Or just type assert the response data to any
console.log( (<any>WORKFLOW_DATA).testDataArray );
Typescript expects WORKFLOW_DATA
to be Object
here:
.subscribe( WORKFLOW_DATA => {} )
because you told it so:
getWorkflowForEditor(): Observable<Object>
But Object
doesn't have testDataArray
property... You should either tell TypeScript that data can have any properties:
getWorkflowForEditor(): Observable<any>
or use
console.log( WORKFLOW_DATA["testDataArray"] );