Type checking of a variable in typescript not working

I am new to typescript, I wrote the code as below

export class RmdRequest {
    public amount: string;
    public age: number;
}

      

in my form I like

in my component i have below

 onSubmit(formValues:RmdRequest){ }

      

formValues ​​is an object as shown below

{amount: 20, age: "Super Hot"}

      

If you see the amount I declared as a string, but pass it as an int. I expected it to throw something out. Am I doing something wrong for validation?

+3


source to share


1 answer


Types that you add only for compilation and are erased by the TypeScript compiler at runtime, so execution checking is not done at runtime. This is how your code method looks at runtime after compilation:

onSubmit(formValues) {
}

      



If you want to do the actual validation on form input, there are many options in the official documentation .

+2


source







All Articles