I have added the "noImplicitAny" and "noImplicitReturns" to my Typescript tsconfig.json file:
{
"compilerOptions": {
"target":"es5",
"noImplicitAny": true,
"noImplicitThis": true,
"noImplicitReturns": true,
"noUnusedLocals":true,
"out": "dist/js/main.js"
}
}
I expected that the following code would generate errors, or at least warnings:
private randomMove() { // no return type but no warning :(
let o = 3; // no type for o but no warning :(
}
The "noUnusedLocals" IS working.
Is this how it's supposed to work, am I missing something? Is it possible to have Visual Studio Code generate warnings when you don't specify types / return types?
You misunderstood what those flags mean.
noImplicitAny:
Raise error on expressions and declarations with an implied any type.
This isn't the case in your example because the compiler infers that the type of o
is number
, you should get the error if you do:
let o;
noImplicitReturns:
Report error when not all code paths in function return a value.
Your function might not need to return at all, but doing this:
function fn(a: number): boolean {
if (a > 0) {
return false;
}
}
Should result in a compilation error.
No, there's no way (that I'm aware of) that will cause the compiler to error if a function doesn't include a return type.
That's because:
: void
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With