2

I've a "constants.ts" file which has below code:

export const PLACEMENT_ID = 'placementId';
export const TIMESTAMP = 'timeStamp';

In my type script code, I'm trying to use the constants as keys while creating a dictionary and then convert it to JSON to write it to a file.

import * as c from './constants';

let obj = JSON.stringify({ c.PLACEMENT_ID: "bob", c.TIMESTAMP: 34 });
console.log(obj);
let parsedData = JSON.parse(obj);

console.log(parsedData[c.PLACEMENT_ID]);
console.log(parsedData[c.TIMESTAMP]);

Its throwing error:

An object literal cannot have multiple properties with the same name in strict mode.

I couldn't find any particular reason why is it throwing that error.

Satyam
  • 15,493
  • 31
  • 131
  • 244
  • @GarethMa, My result is same... I want to place the keys in the constants file so that I can use them in multiple files. I can't hard code them as "{ 'placementId': "bob", 'timeStamp': 34 }" – Satyam Apr 01 '20 at 13:21

1 Answers1

2

It works fine:

let obj = JSON.stringify({ [c.PLACEMENT_ID]: "bob", [c.TIMESTAMP]: 34 });