You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We have a lint rule which attempts to check if a non-null assertion operator is required or not, and warns against the unnecessary operator.
We've essentially got this check in place currently:
consttype=checker.getTypeAtLocation(nonNullNode.expression);if(type===checker.getNonNullableType(type)){reportAndFix('non-null assertion is not required');}
Which works for every case I can think of, except for the "use before assigned" case.
In that case the type returned by the checker is non-null unless the original variable declaration includes a nullable type.
In this case, the type is considered non null, and the identifier is considered not yet assigned.
Example code:
functiontestRequired(){letresolve: ()=>voidnewPromise(resolve0=>{resolve=resolve0})returnresolve!// this non-null assertion is required, removing it causes compiler to hard fail}functiontestNotRequired(){letresolve: ()=>void=()=>{}newPromise(resolve0=>{resolve=resolve0})returnresolve!// this non-null assertion is not required, removing it is fine}
I'm trying to figure out if it's possible to detect (using the type checker or similar) that the non-null operator is truly required because the variable is being used before it is assigned.
If we can't detect this case, then we can't provide an autofixer (and I'd really like to provide a fixer), because otherwise in this case we fix to non-compiling code.
We have a lint rule which attempts to check if a non-null assertion operator is required or not, and warns against the unnecessary operator.
We've essentially got this check in place currently:
Which works for every case I can think of, except for the "use before assigned" case.
In that case the type returned by the checker is non-null unless the original variable declaration includes a nullable type.
In this case, the type is considered non null, and the identifier is considered not yet assigned.
Example code:
I'm trying to figure out if it's possible to detect (using the type checker or similar) that the non-null operator is truly required because the variable is being used before it is assigned.
If we can't detect this case, then we can't provide an autofixer (and I'd really like to provide a fixer), because otherwise in this case we fix to non-compiling code.
related: typescript-eslint/typescript-eslint#453
The text was updated successfully, but these errors were encountered: