-
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
Suggestion: Let null assertion (!
) operator block "used before assigned" errors
#11463
Comments
Yes, some solution to immediately invoked callbacks would be great. @novemberborn and others have had issues trying to convert a const noop = () => { };
function createDeferral() {
let complete = noop;
let cancel = noop;
const promise = new Promise<void>((resolve, reject) => {
complete = resolve;
cancel = reject;
});
return { complete, cancel, promise };
}
const deferral = createDeferral(); Which sort of seems silly. I assume we would rewrite it as this with this proposal: function createDeferral() {
let complete: () => void;
let cancel: () => void;
const promise = new Promise<void>((resolve, reject) => {
complete = resolve;
cancel = reject;
});
return { complete!, cancel!, promise };
}
const deferral = createDeferral(); I also second the "undocumented" nature of |
As I mentioned in #11498 I am more supportive of an annotation that indicates that the function is immediately invoked than this solution. |
Approved since "not assigned yet" is really the same thing as "still has |
TypeScript Version: nightly (2.1.0-dev.20161008)
Code
(Compile with
--strictNullChecks
)Expected behavior:
No error
Actual behavior:
a.ts(5,1): error TS2454: Variable 'n' is used before being assigned.
The problem is fixed by using
let n: number | undefined
, but it's not obvious that that's the solution, and it would be intuitive for the!
operator to work in this situation.Aside: I don't see the
!
operator documented in the handbook.The text was updated successfully, but these errors were encountered: