-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Question: Should let array = []
really mean we want a never[]
instead of any[]
or is it just my compiler options?
#18687
Comments
let array = []
really mean we want a never[]
instead of any[]
or is it just me?let array = []
really mean we want a never[]
instead of any[]
or is it just my compiler options?
This is caused by the combination of The particular combination of settings means that we don't allow ourselves to use control flow analysis or allow the array to be implicitly I'd recommend turning off |
I got a chance to try the various combinations and it is consistent. Is there a way to make such combinations — ones that would have predictable but maybe not intuitive side effects from a user's perspective — standout or be flagged to make them realize that. I am going with an assumption that array defaulting to never is probably not a desired type-checking configuration for most users. Maybe if the type-checker description for something is not never may trigger reference the options and indicate the face that those two options together are a potential mistake. Thanks :) |
@RyanCavanaugh would it be practical to point out in some diagnostic output that those two options together have such implications? |
I'm not sure that's a long-term tractable solution. There are about 80 flags so even conservatively that's 80 * 79 = 6,300 flag combinations which are potential traps to think about. As far as I know you're the first person to notice this, so on balance it's probably not worth handling. We can reconsider if more people find odd combinations of flags that have bad results. |
I just really hope there aren't actually 6,320 traps out there 👍 |
Me too 😅 |
To follow up on this issue, with TSC 2.7.1 ( let a = [] // any[]
a.push(1) // string[]
a.push('red') // (number | string)[]
|
@bcherny it's not |
@RyanCavanaugh Right. I guess to rephrase, it looks like I can keep widening let a = [] // any[]
a.push(1) // number[]
a.push('red') // (number | string)[]
a.push(true) // (number | string | boolean)[]
a.push(a) // (number | string | boolean | (number | string | boolean)[])[] I still get safety when reading a member of |
It's an improvement on the existing behavior because it gives fewer errors but catches the same type mismatches. Intuitively, this code let a = []
a.push(1)
a.push('red') should not behave any differently from let a = [1, 'red'] since we know they're equivalent. |
I see. So when That makes a lot of sense, thanks @RyanCavanaugh! |
TypeScript Version: 2.5.2
Code
Compiler Options
Expected behavior
» tsc⏎ # a pause of nothingness
Actual behavior
Potential causes
After some testing, I did pinpoint the culprit behind this problem in my compiler options:
It seems that setting
noImplicitAny
totrue
does not suffer from this issue but for some reasonfalse
seems to result in the opposite effect of what is expected.So in my opinion, somewhere in related to parsing this particular compiler option there might be some assumption related to the various possible values for the tri-states of
noImplicitAny
.Sample Project:
typescript-empty-array-issue.zip
» yarn && yarn build
Thanks!
The text was updated successfully, but these errors were encountered: