0

Is there a way to compile TypeScript parameter type to type checking if-statements to prevent runtime errors like this:

// TypeScript file
function test(param: string) {
    return param.toUpperCase();
}

// should become JavaScript file
function test(param) {
    if (typeof param !== "string") {
        throw new Error("param must be of type 'string'");
    }
    return param.toUpperCase();
}
Krupesh Kotecha
  • 2,396
  • 3
  • 21
  • 40
scipper
  • 2,944
  • 3
  • 22
  • 45
  • This does not really answer my question. I am aware of the fact that types are stripped by the TSC. So my question is, if there is a way to add some kind of native JS type check right before types are being stripped. – scipper Nov 13 '18 at 10:05
  • AFAIK by default there is no way to auto add such native JS checks – SET001 Nov 13 '18 at 10:13

0 Answers0