镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Disallow truthiness coercions on known-true/known-false values #9041

Description

Inspired by #7746 but making a new issue for clarity

Bad code:

function func(): boolean { return Math.random() > 0.5; }
if (func) {
  // oops, meant to write func()
}

Change: Under --strictNullChecks, it becomes an error to use an expression in a truthiness position unless the type of the expression is possibly-falsy

Possibly-falsy types are:

  • any
  • The primitives: string, string literal types, number, enum types, and boolean
  • Type parameters
  • Union types containing possibly-falsy types
  • An intersection type where any member is possibly falsy

A truthiness position is:

  • The test expression of an if, while, or do/while
  • The middle expression of a for
  • The first operand of ?, && or ||
  • The operand of !

No activity

Activity on this issue will appear here.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions