0

I trying to inherit or extend const into another const

Parent.ts

export const Vehicle = {
  'Name': 'Honda',
  'NoOfWheels': '4'
}

Child.ts

import { Vehicle } from './Parent';
export const HondaVehicle = {
    'VehicleName': 'Honda City',
    'Color': 'Red',
    'EngineCC': '2100cc',
}

So I am expecting output

{
      'Name': 'Honda',
      'NoOfWheels': '4',
      'VehicleName': 'Honda City',
      'Color': 'Red',
      'EngineCC': '2100cc',
}

So kind help me.

R. Richards
  • 24,603
  • 10
  • 64
  • 64
Bulla
  • 924
  • 1
  • 7
  • 15
  • 3
    The [spread operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax) is made for that ! –  Mar 27 '19 at 10:02
  • 1
    Possible duplicate of [How can I merge properties of two JavaScript objects dynamically?](https://stackoverflow.com/questions/171251/how-can-i-merge-properties-of-two-javascript-objects-dynamically) – bugs Mar 27 '19 at 10:03

1 Answers1

2

As trichetriche said you can use the spread syntax

import { Vehicle } from './Parent';
export const HondaVehicle = {
   'VehicleName': 'Honda City',
   'Color': 'Red',
   'EngineCC': '2100cc',
    ...Vehicle
}

Alternatively you can use Object.assign()

import { Vehicle } from './Parent';
export const HondaVehicle = Object.assign({
   'VehicleName': 'Honda City',
   'Color': 'Red',
   'EngineCC': '2100cc',
}, Vehicle)

This is not exclusive to Angular by the way, it's a javascript thing.

ApusApus
  • 276
  • 1
  • 5