Skip to content

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

Closed
@ivogabe

Description

@ivogabe

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'.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions