Is there a way to automatically convert data from string to number?
I have some unclear data that I want to be converted automatically based on its type.
This is the html code:
<form [formGroup]="formUser">
<label>name: </label>
<input formControlName="name" >
<br>
<label>name: </label>
<input formControlName="userId" >
</form>
<button (click)="send()">send</button>
This is the ts code:
export class AppComponent {
constructor(private fb:FormBuilder){
}
formUser=this.fb.group({
name:null,
userId:null,
})
send(){
const user:IUser=this.formUser.value
console.log(user);
}
}
interface IUser{
name:string,
userId:number
}
Here's what appears in the console:
{name: "alis", userId: "12"}
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
I would prefer not to rely on this method for conversion:
user.userId=Number(user.userId)