Regression: Not-null assertion causes implicit any
See original GitHub issueTypeScript Version: 2.7.0-dev.20171029
Code
function test(numbers: number[]) {
let last;
for (const n of numbers) {
if (n % 2) {
return n;
}
last = n;
}
return last!; // without the bang, last is inferred as `number | null`
// adding the bang causes implicit any
}
Workaround: Initialise the “tracking” variable with undefined.
function test(numbers: number[]) {
let last = undefined;
for (const n of numbers) {
if (n % 2) {
return n;
}
last = n;
}
return last!; // OK
}
Expected behavior: Should compile successfully with and without a bang.
Issue Analytics
- State:
- Created 6 years ago
- Comments:24 (12 by maintainers)
Top Results From Across the Web
A note on TypeScript non-null assertion operator - Medium
It lets you deliberately ignore an expression's possible null -ness on a case-by-case basis.
Read more >Implicit assert statement in Groovy - spock - Stack Overflow
When I execute them, explicit assert statement test fails as expected thanks to assert statement. no assert statement test passes and I would ......
Read more >xUnit2002 flags a literal int that's being cast to a reference type ...
Build the following project which does this: Assert.NotNull((JsonValue?)(int?)42);. The cast here should treat that int as a nullable reference ...
Read more >AssertJ - fluent assertions java library - WebLab
This section describes all the available assertions for CharSequence (including String , StringBuilder , StringBuffer , …): The javadoc for ...
Read more >AssertJ - fluent assertions java library - GitHub Pages
// entry point for all assertThat methods and utility methods (e.g. entry) import static org.assertj.core.api.Assertions.*; // basic assertions assertThat(frodo ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@aminpaks I still see the error in the Typescript playground with the 4.1.3 (current latest version). Your code snippet is missing the non-null assertion (
return last;
instead ofreturn last!;
).Is anyone still on it? Can I give it a shot as a first issue?