1

I'm trying to define a mock array of objects in Typescript 2.3.3 (Angular 4), but I'm getting errors.

My main data class is defined in a file called invoice-config.ts:

import {CustomerVariant} from './customer-variant'                              

export class InvoiceConfig {                                                    
  customerName: string;                                                         
  customerVariants: CustomerVariant[];                                          
}

These are the contents of customer-variant.ts:

export class CustomerVariant {                                                                                                         
  id: string;                                                                  
  templates: string[];                                                 
} 

Now, I would like to create a mock array of InvoiceConfig objects in a file called mock-invoice-configs.ts. I've tried with this file:

import { InvoiceConfig } from './invoice-config';                               

export const INVOICE_CONFIGS: InvoiceConfig[] = [                               

  {                                                                             
    customerName: "CUSTOMER1",                                                  
    customerVariants = [                                                        
      {                                                                                                     
        id: "A9",                                                       
        templates = [                                                  
          "default"                                                             
        ]                                                                       
      }                                                                         
    ]                                                                           
  },                                                                            

  {                                                                             
    customerName: "CUSTOMER2",                                                        
    customerVariants = [                                                        
      {                                                                                                            
        id: "A3",                                                       
        templates = [                                                  
          "default"                                                             
        ]                                                                       
      }                                                                         
    ]                                                                           
  }
]     

But it produces errors:

ERROR in /home/myuser/client-app/src/app/mock-invoice-configs.ts (7,5): Cannot find name 'customerVariants'.

ERROR in /home/myuser/client-app/src/app/mock-invoice-configs.ts (7,22): '=' can only be used in an object literal property inside a destructuring assignment.

ERROR in /home/myuser/client-app/src/app/mock-invoice-configs.ts (19,5): Cannot find name 'customerVariants'.

ERROR in /home/myuser/client-app/src/app/mock-invoice-configs.ts (19,22): '=' can only be used in an object literal property inside a destructuring assignment.

I don't understand why it cannot find 'customerVariants' (is one of the properties of the InvoiceConfig class?). And how can I define an array of nested objects (customerVariants) without using '=' ?

1
  • 1
    use : instead of it? as you would for any other property.. because it's the same as any other property. Commented Jun 20, 2017 at 21:29

1 Answer 1

4

You need to replace = with : E.g.

export const INVOICE_CONFIGS: InvoiceConfig[] = [ { 
    customerName: "CUSTOMER1", 
    customerVariants: [ { id: "A9", templates: [ "default" ] } ] }
]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.