I am using VS Code and it helps a lot when working with Typescript as it tells you when current issues there are without needing to transpile.
I am using Typescript with nodeJS and it works really well.
The only issue I have is so called "null / undefined checking functions".
See this example:
class User {
public someProperty: string | undefined;
// ...
public get canDoSomething() {
return this.someProperty != undefined;
}
}
let myUser: User = ...
This works nicely:
if (myUser.someProperty != undefined) {
// at this point myUser.someProperty is type string only (as it cannot be undefined anymore
}
However this fails
if (myUser.canDoSomething) {
// because typescript still thinks that myUser.someProperty is string | undefined and not just string, even though this method checks for that.
}
Any idea how I would tell typescript? Because sometimes a method like that is cleaner than keep comparing the properties to undefined itself.
Thanks
ifstatement be:if (myUser.canDoSomething())? Without the parentheses, you're not invoking the type guard, just checking whether the function is defined or not. From TypeScript's perspective,somePropertycan still beundefined.get