Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Inconsistency with boolean negation as void type guards with --strictNullChecks #33180

Copy link
Copy link
@timhwang21

Description

@timhwang21
Issue body actions

TypeScript Version: typescript@3.7.0-dev.20190831

Search Terms: void guard strictNullChecks boolean negation refinement

Code

// Toggle strictNullChecks off and see the type error appear
// on line 7, while 2nd fn never errors and 3rd always errors
const getMaybeStringLen = (maybeString: string | void) => {
  if (maybeString === null || maybeString === undefined) {
    return undefined;
  }
  return maybeString.length;
};

const getMaybeStringLenBoolNegation = (maybeString: string | void) => {
  if (!maybeString) {
    return undefined;
  }
  return maybeString.length;
};

const getMaybeStringLenBoolCoercionNegation = (maybeString: string | void) => {
  if (!Boolean(maybeString)) {
    return undefined;
  }
  return maybeString.length;
};

Expected behavior:

  • strictNullChecks: false behaves less strictly than strictNullChecks: true. Admittedly a loose criterion, but it seems like the stricter check fails with strictNullChecks: false and passes with strictNullChecks: true, while the type coercive check passes in both cases.

My confusion stems from the fact that ! is a valid type guard for void for both compiler flags, while checking against null and undefined is valid for strictNullChecks: true only.

As a bonus, while ! is always valid, !Boolean(x) is never valid.

Actual behavior:

  • getMaybeStringLen...
    • ...typechecks with strictNullChecks: true
    • ...does NOT typecheck with strictNullChecks: false
  • getMaybeStringLenBoolNegation...
    • ...typechecks with strictNullChecks: true
    • ...typechecks with strictNullChecks: false
  • getMaybeStringLenBoolCoercionNegation...
    • ...does NOT typecheck with strictNullChecks: true
    • ...does NOT typecheck with strictNullChecks: false

Playground Link: http://www.typescriptlang.org/play/#code/PTAEBUHsHNoGwKagM4BcBOBLAxqgcgK5xwDCAFgtgNbKiQBm9oAhgHYAmKCSqFoqATwAOSBOnSR0LISOboAUCDqtQcTKyQB2ADSgA7mUyJQAJg6h6KjQDcxoMRPS02nAMzpOzOHuYDaDyWR5bEhWNFBoBFQAWV8AIwQAZQx1aAAZBBUAXlAACgBbeKSU1mgALhQS6FAAH1BrSEx2AEpQLIA+UABveVBQTCYCouSsUrasnNYiOFq6woEEkdTxnIIOBHp1BBbu3r7QdCiCdBU19g2t9gBuPYBfPcPUY5V5xaqAOkRS3hvbm+DQuFIjFhlUMqwAEKQSBwPAIaDMVCYUJtPKvYqjcqVTG1eqNHYdXZ9AZ5ACE6KWpVaPX2ByOJ1AZwuGmudwe9JeoMxn0y0B+8j+8gBYVQESisQWGNS4KhMJIkDE2GRrDhCKRKJyQ0llKxaBxdQaTVahJp-UGpNliDYWremOa1L2fUez0Z602LJufXuTo5oApHy+fLIvxuQA

Related Issues: #1806, #8322, #10564

Reactions are currently unavailable

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptA bug in TypeScript

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.