Property '' does not exist on type 'Object'. Observable subscribe

后端 未结 3 1455
时光说笑
时光说笑 2021-01-01 16:24

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         


        
相关标签:
3条回答
  • 2021-01-01 16:57

    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.

    0 讨论(0)
  • 2021-01-01 17:00

    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:

    1. Type the data and return type differently

      WORKFLOW_DATA: { testDataArray: any } = []
      
      getWorkflowForEditor(): Observable<{ testDataArray: any }>
      
    2. Or just type assert the response data to any

      console.log( (<any>WORKFLOW_DATA).testDataArray );
      
    0 讨论(0)
  • 2021-01-01 17:03

    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"] );
    
    0 讨论(0)
提交回复
热议问题