้•œๅƒ็ซ™็‚น ยท ๆœฌ้กต็”ฑ็ฌฌไธ‰ๆ–น GitHub ๅช่ฏป้•œๅƒๆไพ›๏ผŒ้ž GitHub ๅฎ˜ๆ–น็ซ™็‚น๏ผŒไธๆŽฅๅ—ไปปไฝ•็™ปๅฝ•ๆˆ–ๅ‡ญๆฎ่พ“ๅ…ฅใ€‚ๅ‰ๅพ€ github.com
Skip to content

Yielding an assertion function call does not narrow the asserted typeย #55205

Description

Bug Report

๐Ÿ”Ž Search Terms

generator, yield, assertion function

๐Ÿ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about assertion functions and generators.

โฏ Playground Link

Playground link with relevant code

๐Ÿ’ป Code

 function * testFunc() {
    type SomeType = {a: number};

    let count = 0;
    function assertAThing(obj: unknown): asserts obj is SomeType {
        // don't care about the impl
        count++;
    }

    const bla: unknown = {a: 3};
    assertAThing(bla);

    // no type error here
    console.log(bla.a);

    const bla2: unknown = {a: 4};
    yield assertAThing(bla2);

    // bla2 is unknown, but should be SomeType as asserted by the assertAThing call.
    if (bla2.a !== 4) {
        throw new Error("bla2.a should be 4.");
    }

    if (count !== 2) {
        throw new Error("Count should be two.");
    }

    console.log("Javascript success")
 }

for (const item of testFunc()) {
    //
}

๐Ÿ™ Actual behavior

The variable bla2 is still unknown even after passing it through the assertion function assertAThing.

๐Ÿ™‚ Expected behavior

bla2 should be narrowed to be SomeType like bla1.

Workaround

We are effectively yielding void since assertion functions cannot return anything. This means there is a simple workaround:

assertAThing(bla2);
yield;

Activity

  1. MartinJohns commented on Jul 30, 2023

    @MartinJohns
    Contributor

    This is not a bug as per #32695:

    A function call is analyzed as an assertion call or never-returning call when

    • the call occurs as a top-level expression statement, and
    • the call specifies a single identifier or a dotted sequence of identifiers for the function name, and
    • each identifier in the function name references an entity with an explicit type, and
    • the function name resolves to a function type with an asserts return type or an explicit never return type annotation.
  2. RyanCavanaugh commented on Aug 1, 2023

    @RyanCavanaugh
    Member

    This doesn't seem like something you would likely (ever?) actually do in real code, and making new CFA graph nodes slows down everything else.

  3. typescript-bot commented on Aug 4, 2023

    @typescript-bot
    Contributor

    This issue has been marked as "Not a Defect" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

  4. locked as resolved and limited conversation to collaborators on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Not a DefectThis behavior is one of several equally-correct options

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions