-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Should error when unwrapping an unwrappable variable #8453
Comments
I think you meant : let s: string;
s = '';
if (!s) { // should throw an error
} That said it can cause issues as people might try defensive coding due to external influences e.g. #8452 now allows people to do |
@basarat I actually meant |
Isn't it a syntax error? |
Got it. I really should have read #7140 better instead of winging it 🌹 Did spend some time searching for "unwrap operator" :) |
Sorry might have used the wrong terminology. Non-null assertion operator is more correct. |
We allow benign coercions everywhere else (e.g. unary |
@RyanCavanaugh I would like if TS did the same with the two example you mentioned. Though I think the unwrap operator is different. It is more common. Actually I found out that had a case where a variable where most of the time of type Besides refactoring benefits, I think it is correct to error just because it is unnecessary operation. |
what about type parameters? |
Can you elaborate? |
function f<T>(a: T) {
return a!; // is this an error or not?
} |
It should error. I think a nullable type argument should be declared at the parameter declaration, because it is more declarative and it is not the call site that decides if it is nullable or not. A nullable type passed in a non-nullable type argument function should also error. function f<T>(a: T) {
return a!; // is this an error or not? yes it is
}
let str: string | undefined;
f(str); //error str is nullable
function f<T>(a: T | undefined) {
return a!; // is this an error or not? no it isn't
} FWIW, that is what swift does. (Interestingly Swift allows nullable passed as an non-null type argument): |
Maybe it is best to expose this feature behind a flag. As CFA improves it will break code that it didn't cover before. In my opinon it is good to error, because devs can learn new CFA:s. |
I think at one point @alexeagle mentioned that if your dependencies weren't updated to use non-null assertions, you could add the assertions as something of a TODO until your dependencies were updated (and if I'm misquoting, feel free to clarify). So it seems like using the assertion is okay in some contexts where you're migrating code, but eventually I think you'd want to get rid of any places where you don't really need those assertions. We should consider how often this scenario pops up. |
We're going to leave this as an OK thing to do. In addition to precedence with other allowed facile assertions, the real risk here is that it could make improvements to the control flow analysis into breaking changes, which would be really unfortunate. |
s
cannot be null or undefined below. So I think it is appropriate to error when trying to unwrap.The text was updated successfully, but these errors were encountered: