question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Regression: Not-null assertion causes implicit any

See original GitHub issue

TypeScript 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:open
  • Created 6 years ago
  • Comments:24 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
eric-hucommented, Jan 9, 2021

@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 of return last!;).

1reaction
Bremyscommented, Apr 8, 2020

Is anyone still on it? Can I give it a shot as a first issue?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found