| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
There was a problem hiding this comment.
We will need some tests for the new token
Sorry, something went wrong.
|
Anders Hejlsberg (@ahejlsberg) what's the best way to add suppression at call site, not globally for the variable? See your second example, like this: function f() {
let x: number; <---- reluctant to suppress checks for real code
doSomethingWithCallback(() => {
x = 1;
});
console.log( x as x! ); <----- prefer to suppress checks at diagnostic call site only
} |
Sorry, something went wrong.
|
mihailik I think you are looking for the pre-existing not-null assertion: console.log( x! ) |
Sorry, something went wrong.
|
Marin Marinov (@gcnew) great, but would that work in this case? |
Sorry, something went wrong.
|
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. |
Sorry, something went wrong.
|
Marin Marinov (@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. Thanks for the syntax!! |
Sorry, something went wrong.
|
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 I know, I know... ~#9998? 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. |
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
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:
Related issues: #11463, #12855, #13811.