-
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
strictNullChecks still allows to assign undefined to the variable declared as number #11238
Comments
This is working as intended. We don't attempt to validate that array indexing operations produce existing elements; rather, we assume they always do. While this is technically not correct, the alternative of always adding |
I would recommend adding this information to docs (handbook + what's new) because it's not the first time this issue is submitted to GitHub - and I guess people don't try to search in closed issues to find all the duplicates - and first thought is that this is in fact TS issue - and it's not easy to find that comment #7140 (comment) in Pull Request comments that explains that it's made for the convenience reason :) Or maybe |
Ok, I see this is a tough problem. But for the end-user/programmer, it means that if my function, for example, accepts a number parameter I still must do guard checks whether it is undefined or not, even with --strictNullChecks, because my language does guaranty nothing. Probably I just don't understand the value proposition of strictNullChecks... |
People don't search the docs either. 😢 Consider the alternative universe where we did say you had to guard-off const arr = [1, 2, 3];
for (let i = 0; i < arr.length; i++) {
console.log('Element ' + i + ' has value ' + (arr[i]!).toFixed());
} with the This doesn't actually help anyone. You can still write this const arr = [1, 2, 3];
for (let i = 0; i <= arr.length; i++) {
console.log('Element ' + i + ' has value ' + (arr[i]!).toFixed());
} or const arr = [1, 2, 3];
for (let i = 0; i < arr.length; i++) {
console.log('Element ' + i +
' has value ' + (arr[i]!).toFixed() +
' and the next one is ' + (arr[i + 1]!).toFixed() );
} or any other number of ways to get it wrong. You know how your car beeps at you if you don't have your seatbelt on? This is useful only because it can tell if you have your seatbelt on or not. A non-useful check would be that when you started the engine, you had to press a button that said "I have my seatbelt on". You'd immediately get into the habit of pressing that button regardless of whether you were actually buckled in. That's basically safety-by-EULA. Same here. |
TypeScript Version: 2.0.0
Code
Expected behavior:
error TS2322: Type 'undefined' is not assignable to type 'number'
Actual behavior:
after compile with command
tsc --strictNullChecks
Runtime exception: Cannot read property 'toString' of undefined
I thought strictNullChecks a there to prevent developer from seeing such kind of exceptions in run time.
The text was updated successfully, but these errors were encountered: