Is there a way to convert non-null assertions in TypeScript into JavaScript that throws an error?
By default, the non-null assertion is ignored (playground):
// Typescript:
function foo(o: {[k: string]: string}) {
return "x is " + o.x!
}
console.log(foo({y: "ten"}))
// Compiled into this js without warnings:
function foo(o) {
return "x is " + o.x;
}
console.log(foo({ y: "ten" }));
// output: "x is undefined"
I am looking for a solution or extension that can make it compile into the following:
function foo(o) {
if (o.x == null) { throw new Error("o.x is not null") }
// console.assert(o.x != null) would also be acceptable
return "x is " + o.x;
}
Is there a method to transform non-null exclamation point assertions into JavaScript assertions or errors?