You have to remember that Typescript transpiles to javascript. In Javascript, the default value of an unassigned variable is undefined
, as defined here.
For example, the following typescript code:
let a: string; console.log(a);
will transpile to the following javascript and logs undefined
.
var a; console.log(a);
This also applies when you pass parameters to a function or a constructor of a class:
// Typescript
function printStr(str: string) {
console.log(str);
}
class StrPrinter {
str: string;
constructor(str: string) {
this.str = str;
console.log(this.str);
}
}
printStr();
let strPrinter = StrPrinter();
In the code example above, typescript will complain that the function and the class constructor is missing a parameter. Nevertheless, it will still transpile to transpile to:
function printStr(str) {
console.log(str);
}
var StrPrinter = (function () {
function StrPrinter(str) {
this.str = str;
console.log(this.str);
}
return StrPrinter;
}());
printStr();
var strPrinter = StrPrinter();
You might also want to see how typescript transpiles to javascript here.