You are correct - here is a fully working example - you'll see that var result
is implicitly a string because the return type is specified on the greet()
function. Change the type to number
and you'll get warnings.
class Greeter {
greeting: string;
constructor (message: string) {
this.greeting = message;
}
greet() : string {
return "Hello, " + this.greeting;
}
}
var greeter = new Greeter("Hi");
var result = greeter.greet();
Here is the number example - you'll see red squiggles in the playground editor if you try this:
greet() : number {
return "Hello, " + this.greeting;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…