We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
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
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.
!
/ Compile with --strict class C { data!: string; / Suppress strict initialization check setData(value: string) { this.data = value; } getData(): string { return this.data; } }
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.
setData
getData
data
--strictPropertyInitialization
function f() { let x!: number; / Suppress definite assignment check doSomethingWithCallback() => { x = 1; }); console.log(x); }
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.
doSomethingWithCallback
x
console.log(x)
Definite assignment assertions are permitted only in the following cases:
let
var
Related issues: #12855, #13811.
Sorry, something went wrong.
Definite assignment assertion '!' on variable and property declarations
1624e1b
Accept new baselines
546663a
Allow '!' only on variable declarations within variable statements
2720763
Merge branch 'master' into definiteAssignmentAssertions
ed4dc57
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
We will need some tests for the new token
Add tests
54d35b9
02fd11e
9b9f3f2
9abb72d
@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 }
@mihailik I think you are looking for the pre-existing not-null assertion:
console.log( x! )
@gcnew great, but would that work in this case?
@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.
console.log
Thanks for the syntax!!
Any chance this feature could expand a bit to solve ~A2 as well?
A2
.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.
TS1255
RyanCavanaugh Awaiting requested review from RyanCavanaugh
mhegazy mhegazy approved these changes
Successfully merging this pull request may close these issues.