As someone who is still relatively new to coding, I am facing a challenge while trying to create a TypeScript class within an Angular project that is based on a complex JSON file. The issue arises from the way the properties were defined in the JSON using string literals. I am uncertain if this is the root cause of the errors I am encountering, and I am seeking guidance on the correct way to declare these properties in TypeScript. Interestingly, when I declare the properties explicitly, everything appears to be in order...
// Initial property declarations seem to be correct
export class State {
'FIPS Code': number;
'Postal': string;
'Area Name': string;
'Less than a high school diploma, 1970': number;
...
}
However, when attempting to create a constructor function, I am faced with various errors...
// Errors occur with all parameter identifiers indicating 'identifier expected'
constructor('FIPS Code': number, 'Postal': string,
'Area Name': string,
'Less than a high school diploma, 1970': number,
'High school diploma only, 1970': number,
...) {
// Type '"FIPS Code"' is not assignable to type 'number'
this['FIPS Code'] = 'FIPS Code';
// The next two assignments are successful, presumably because they are strings
this['Postal'] = 'Postal';
this['Area Name'] = 'Area Name';
// The remaining assignments result in errors indicating not assignable to type 'number'
this['Less than a high school diploma, 1970'] = 'Less than a high school diploma, 1970';
...
}