Thanks for your patience here, I'm just starting out with TypeScript.
I'm working on an angular 2 app that needs to accept text inputs and then make a bunch of calculations. I (incorrectly?) assumed that i would need to first bind inputs to "any" type variables within my data model and then convert those variables to numbers in order to crunch numbers. I've looked all around, and can't find how to do this in such a way that it doesn't throw this TS compiler error:
`src/calculator_service.ts(40,5): error TS2322: Type 'number' is not assignable to type 'string'.`
Within my CalculatorService, I have this function:
/*
* Convert the object of strings recieved from the form into a clean object of integers
*/
n(model:ModelFields) {
// Clone it
this.numericModel = Object.assign({}, this.model);
for (var prop in this.numericModel) {
if (this.numericModel.hasOwnProperty(prop)) {
// strip off all non-numeric charactersklj
this.numericModel[prop] = this.numericModel[prop].replace(/\D/g,'');
// convert to Any typescript type
// this breaks the application, and still throws a compiler error. nope.
// this.numericModel[prop] = this.numericModel[prop]:Any;
// convert to Number type
// this gives a typescript console error, but seems to still compile...
// ignoring this for now in order to meet deadline
this.numericModel[prop] = +this.numericModel[prop];
}
}
return this.numericModel;
}
and the ModelFields definition (thanks tarh!)
export class ModelFields {
constructor(
public fieldName: any,
public anotherField: any
)
{}
}
Any ideas? Thanks everybody!
ModelFields?export class ModelFields { constructor( public fieldName: any, public anotherField: any ) {} }