Skip to content

No errors on array initialization if any element is of any type #46190

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

Closed
chrisirhc opened this issue Oct 3, 2021 · 4 comments
Closed

No errors on array initialization if any element is of any type #46190

chrisirhc opened this issue Oct 3, 2021 · 4 comments

Comments

@chrisirhc
Copy link

chrisirhc commented Oct 3, 2021

Bug Report

πŸ”Ž Search Terms

  • any type used in array initialization

πŸ•— Version & Regression Information

3.9.7 and beyond

⏯ Playground Link

https://www.typescriptlang.org/play?ts=3.9.7#code/MYewdgzgLgBBIFsCmYCuCBGSBOAuGamOMAvDAIwDcAUKJLPMlABYCWYA5kgDYRL4BDMAE9SFGnWgxg+aNnYcA2gF0xixinRZsAGjiIkLBTz7LKQA

πŸ’» Code

const somenumber: number = 1;
const somethingelse: any = 1;
const c: string[] = [somenumber, somethingelse];

πŸ™ Actual behavior

No errors reported.

πŸ™‚ Expected behavior

Error reported on the array initialization, on line 3, on the use of somenumber in a string array .

Side note

If this is expected behavior when any item is any, is there a way to have the typescript lint report this?
It seems odd that the initialization line reports no error or warning at all, since there's a specific type declared on that line.

@chrisirhc chrisirhc changed the title No errors on array assignment if any element is of any type No errors on array initialization if any element is of any type Oct 3, 2021
@IllusionMH
Copy link
Contributor

When you are using any you basically opt out of type checking in that place and it's intentionally that there are no errors and can't be.

If you want to mark variable that can keep some value but you don't know what it is exact - use unknown.

@chrisirhc
Copy link
Author

chrisirhc commented Oct 3, 2021

There was a mistake in my demo code/playground. I've updated it.
The issue isn't with the any variable, but with the number variable being unchecked.

I understand if somethingelse is not being checked, but I would've expected that the first element, somenumber, be checked.

This can be especially hard to find if the somethingelse was an inferred any rather than an explicit any.

@MartinJohns
Copy link
Contributor

Duplicate of #19541. Used search terms: array any in:title

@chrisirhc
Copy link
Author

Thanks folks. Closing since this is also an exact duplicate of #38870 , which was closed as dupe of #19541. Sorry, I didn't find that earlier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants