Skip to content

Boolean function inference has incorrect behavior #51184

@scottmas

Description

@scottmas

Bug Report

Typescript is unable to correctly determine that a function's return satisfies the specified type.

🔎 Search Terms

"Typescript expanding boolean into true/false" (#30029)

🕗 Version & Regression Information

v. 4.7.4

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about: YES

⏯ Playground Link

https://www.typescriptlang.org/play?ts=4.9.0-dev.20221014#code/LAKALgngDgpgBAIQIYBMBiA7OBeOAKPAShwD44wAnAVxmIB98jS4AzJAGwGdaBuUAYwD2GTmHIxRALkSpMOfMWxkA3qDhwKMMFQpYAskjAALAHQUkGFIIC2TMgAY+IAL48gA

💻 Code

type SomeInferredFunctionType = (() => true) | (() => false); 

// Gives TS error!
const test: SomeInferredFunctionType = () => { 
  return Math.random() > 0.5;
};

🙁 Actual behavior

Typescript thinks test does not satisfy its given type when it does. Note, that it is possible (with much difficulty) to collapse SomeInferredFunctionType into the correct simplified type() => boolean when I have direct access to the type. However, in my codebase, the type is actually automatically inferred and I do not have access to it and I am just consuming it.

🙂 Expected behavior

Typescript should be able to determine that test satisfies the condition given by SomeInferredFunctionType

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions