-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
--noImplicitAny error not reported for variable declaration #30899
Comments
There is no Intuitively, this is entirely consistent with you writing let x = Date.now() ? 1 : { obj: true }; which is not an error either. |
That does not seem correct to me. The programmer can assign x to just about anything, which makes it the same as if they had written: let x;
for (const a of []) {
x = 1;
}
if (Date.now()) {
x = new Date();
}
switch (Date.now()) {
case 1: x = ''; break
case 2: x = () => {} break
}
foo(x);
function foo(y) { } Type inference will make sure but the assignments themselves are the problem we are trying to catch |
Why should that code have an error, but not this code? The problem here is for (const a of []) {
foo(1);
}
if (Date.now()) {
foo(new Date());
}
switch (Date.now()) {
case 1: foo(''); break
case 2: foo(() => {}) break
}
function foo(y) { } This is an introduced feature (#11263) we did on purpose to reduce spurious |
Thanks for the link to the issue. I can better understand why this decision was made but still I'm not convinced from a language user perspective that this For example, thy should be an implicit any error: function foo(x?) {
if (Date.now()) {
x = 'str';
}
return bar(x);
}
function bar(x: any) { } while this is not: let x;
if (Date.now()) {
x = 'str';
}
bar(x)
function bar(x: any) { } |
Because one of them creates a usable |
You should think of it in these terms: Was I allowed to access a property of an function foo(x?) {
x.asdf; // yes
} vs let x;
x.asdf; // no
if (Date.now()) {
x = 'str';
x.asdf; // no
}
bar(x)
x.asdf; // no
function bar(x: any) {
x.asdf; // I *did* write :any
} |
Consider an example like: const parseError = (err: Error) => {
let errData;
try {
errData = JSON.parse(err.message);
// log errData
} catch (e) {
// log failure
}
if (errData['some-key'].includes('403')) {
throw new Error('some error message');
}
throw err;
}; When we go to access |
@lallenlowe you have a different problem than the one described here. errData = JSON.parse(err.message) as { [k: string]: string }; or similar |
@RyanCavanaugh OK, yeah, I see how it is different now. Of course I can cast that type, I just wish TS could enforce this because it is all over our repo now. I think the correct solution is probably that |
I also think it is strange, because the options |
For anybody find a eslint solution for this: https://stackoverflow.com/questions/73440042/typescript-prevent-inferring-types-except-at-variable-declaration |
TypeScript Version: 3.5.0-dev.20190412
Search Terms:
Code
Compile the following ts with
--noImplicitAny
Expected behavior:
The declaration
let x
should produce an implicit any errorActual behavior:
No error reported. After the assignment, the type of
x
isnumber | { obj: boolean }
Playground Link:
https://www.typescriptlang.org/play/#src=let%20x%3B%0D%0Aif%20(Date.now())%20%7B%0D%0A%20%20%20%20x%20%3D%201%3B%0D%0A%7D%20else%20%7B%0D%0A%20%20%20%20x%20%3D%20%7B%20obj%3A%20true%20%7D%3B%0D%0A%7D%0D%0A%0D%0Afoo(x)%3B%0D%0A%0D%0Afunction%20foo(y)%20%7B%20%7D
Related Issues:
From microsoft/vscode#72193
The text was updated successfully, but these errors were encountered: