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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This PR introduces the ability for a variable or property declaration to include a definite assignment assertion in the form of a ! character following the variable or property identifier. A definite assignment assertion instructs the control flow analyzer to always consider the variable definitely assigned, even when the analyzer is unable to prove it.
In the example above, if it is known that setData will always be called before getData, there is no need to initialize the data property upon construction. However, the control flow analyzer can't make that assumption, and in --strictPropertyInitialization mode (see #20075) it reports an error unless a definite assignment assertion ! is included.
In the example above, if it is known that doSomethingWithCallback will always invoke the callback before returning, there is no need to initialize x in its declaration. However, the control flow analyzer conservatively assumes that x is used before being assigned in the console.log(x) call and reports an error. A definite assignment assertion can now be included in the declaration to suppress this error.
Definite assignment assertions are permitted only in the following cases:
On a property declaration in a class, provided the property has a type annotation and no initializer and isn't static or abstract.
On a let or var variable declaration, provided the variable has a type annotation and no initializer.
@mihailik I tested it before posting just to make sure and it worked.
Thinking about it for a second time, is your intention to mark properties as "assigned" in a class constructor under --strictPropertyInitialization? If that's the case, I'm afraid this will not assure the compiler that the property has been assigned. I haven't tried it yet, though.
@gcnew I was referring to the second example in the original post. It's console.log used as one-off diagnostics, where in-place assert is preferable to the wide-scoped change.
Any chance this feature could expand a bit to solve ~A2 as well?
.ts(616,8): error TS1255: A definite assignment assertion '!' is not permitted in this context.
.ts(621,14): error TS2365: Operator '!==' cannot be applied to types '0' and '1'.
The terminal process terminated with exit code: 1
Wish: Instead of the TS1255 error when variable declaration has an initial value, TypeScript treats it as a: don't overly-narrow type for later CFA assertion.
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Labels
None yet
5 participants
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR introduces the ability for a variable or property declaration to include a definite assignment assertion in the form of a
!
character following the variable or property identifier. A definite assignment assertion instructs the control flow analyzer to always consider the variable definitely assigned, even when the analyzer is unable to prove it.In the example above, if it is known that
setData
will always be called beforegetData
, there is no need to initialize thedata
property upon construction. However, the control flow analyzer can't make that assumption, and in--strictPropertyInitialization
mode (see #20075) it reports an error unless a definite assignment assertion!
is included.In the example above, if it is known that
doSomethingWithCallback
will always invoke the callback before returning, there is no need to initializex
in its declaration. However, the control flow analyzer conservatively assumes thatx
is used before being assigned in theconsole.log(x)
call and reports an error. A definite assignment assertion can now be included in the declaration to suppress this error.Definite assignment assertions are permitted only in the following cases:
let
orvar
variable declaration, provided the variable has a type annotation and no initializer.Related issues: #11463, #12855, #13811.