Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Double errors reported with control flow based checking in a loop #8404

Copy link
Copy link
@ivogabe

Description

@ivogabe
Issue body actions

In a loop, multiple errors are sometimes reported on the same location, but with slightly different types.

@ahejlsberg Any idea what's going on?

TypeScript Version:

nightly (1.9.0-dev.20160430)

Code

function test<U>(xs: U[], combine: (a: U, b: U) => U) {
    let hasValue = false;
    let value: U | undefined;
    for (const x of xs) {
        if (hasValue) {
            value = combine(value, x);
        } else {
            hasValue = true;
            value = x;
        }
    }
}

Expected behavior:
Only one error should be reported:

value = combine(value, x);
                ~~~~~ Argument of type 'U | undefined' is not assignable to parameter of type 'U'.
                         Type 'undefined' is not assignable to type 'U'.

By adding an exclamation mark to that line (value = combine(value!, x);), no error should be reported.

Actual behavior:
Two errors are reported:

value = combine(value, x);
                ~~~~~ Argument of type 'U | undefined' is not assignable to parameter of type 'U'.
                         Type 'undefined' is not assignable to type 'U'.
                ~~~~~ Argument of type 'undefined' is not assignable to parameter of type 'U'.

Adding an exclamation mark gives:

value = combine(value!, x);
                ~~~~~~ Argument of type 'nothing' is not assignable to parameter of type 'U'.
Reactions are currently unavailable

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptA bug in TypeScriptFixedA PR has been merged for this issueA PR has been merged for this issue

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions

    Morty Proxy This is a proxified and sanitized view of the page, visit original site.