I've an issue in my Angular service with TypeScript type inferance. I'm new to TypeScript and I wonder why this code:
initializeTransaction(user: User, amount: number) {
let params = {
userId: user.id,
amount: amount
};
return this.http.post<any>('/api/v1/payment/transaction/initialize', params);
}
where:
export class User {
id: number;
}
gives me this payload:
{"userId":1,"amount":"2000"}
Shouldn't amount
be a number?
Edit: Here is the context of PaymentComponent:
export class PaymentComponent implements OnInit {
private user = new User();
submitted = false;
redirecting = false;
paymentForm: FormGroup;
constructor(
private formBuilder: FormBuilder,
private paymentService: PaymentService,
private userService: UserService
) {
this.userService.me().subscribe(response => this.user = response);
}
ngOnInit() {
this.paymentForm = this.formBuilder.group({
amount: ['', [Validators.required, Validators.pattern("^[0-9]*$")]]
});
}
get f() { return this.paymentForm.controls; }
onPay() {
this.submitted = true;
if (this.paymentForm.invalid) {
return false;
}
this.redirecting = true;
this.paymentService.initializePage(this.user, this.f.amount.value)
.subscribe(response => window.location.href = response.redirectUrl);
}
}
amount
got to be a string somehow it will still be a string when you assign it toUser
+this.f.amount.value
instead ofthis.f.amount.value
?