Skip to content

Invalid code typechecks in generators with strictNullChecks #22290

Closed
@undeadcat

Description

@undeadcat

TypeScript Version: 2.8.0-dev.20180302

Search Terms: generator, yield, strictNullChecks, typechecking

Code

function* myGenerator(): IterableIterator<number> {
  yield;
  yield 2;
}

for (let n of myGenerator()) {
  let number: number = n;
  console.log(number);
}

tsconfig.json:

{
  "compilerOptions": {
    "target":"es6",
    "strictNullChecks": true,
  }
}

Expected behavior:
I would expect yield; (without expression) to be forbidden if the generator return type (IterableIterator<number>) doesn't allow undefined.

Actual behavior:
This compiles fine, prints 'undefined', '2' (we're essentially allowing undefined to be assigned to non-nullable number).

Playground Link: link

Metadata

Metadata

Assignees

No one assigned

    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