Skip to content

Assertion function must be enforced over flow type inference #36150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
SQReder opened this issue Jan 12, 2020 · 4 comments
Closed

Assertion function must be enforced over flow type inference #36150

SQReder opened this issue Jan 12, 2020 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@SQReder
Copy link

SQReder commented Jan 12, 2020

TypeScript Version: 3.7.2

Search Terms:
callback, never, narrow, assert

Expected behavior:

  1. foo type should be inferred as Foo, as described in the variable declaration
let foo: Foo | null = null;
/* ... */
if (foo != null) {
  /* foo must be Foo type here */
}

Actually, if IIFE used inference behave as expected

let foo: Foo | null = null;

(function () { foo = {} })()

if (foo != null) {
  /* foo is of type Foo here */
}
  1. Assertion function must be preferred over flow type inference

Actual behavior:
foo type inferred as never and even assertion does not change it

Related Issues:
no

Code

function assertNonEmpty<T>(value: T | null | undefined): asserts value is T {
    if (value == null) {
        throw new Error("Should not be null or undefined");
    }
}

function absurd<T extends never>(value: T): asserts value is never { };

type AssertEqual<A, B> = A extends B ? B extends A ? true : never : never;

function runCallback(cb: () => void) { cb() };

type Foo = { __TYPE__?: any };

let foo: Foo | null = null;

runCallback(() => foo = {});

if (foo != null) {
    const shouldBeInferredAsFoo: AssertEqual<typeof foo, Foo> = true;

    assertNonEmpty<{}>(foo);

    const mustBeFoo: AssertEqual<typeof foo, Foo> = true;

    absurd(foo); // foo actually is never
} else {
    const nullish: AssertEqual<typeof foo, null> = true;
}
Output
"use strict";
function assertNonEmpty(value) {
    if (value == null) {
        throw new Error("Should not be null or undefined");
    }
}
function absurd(value) { }
;
function runCallback(cb) { cb(); }
;
let foo = null;
runCallback(() => foo = {});
if (foo != null) {
    const shouldBeInferredAsFoo = true;
    assertNonEmpty(foo);
    const mustBeFoo = true;
    absurd(foo); // foo actually is never
}
else {
    const nullish = true;
}
Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "useDefineForClassFields": false,
    "alwaysStrict": true,
    "allowUnreachableCode": false,
    "allowUnusedLabels": false,
    "downlevelIteration": false,
    "noEmitHelpers": false,
    "noLib": false,
    "noStrictGenericChecks": false,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "esModuleInterop": true,
    "preserveConstEnums": false,
    "removeComments": false,
    "skipLibCheck": false,
    "checkJs": false,
    "allowJs": false,
    "declaration": true,
    "experimentalDecorators": false,
    "emitDecoratorMetadata": false,
    "target": "ES2017",
    "module": "ESNext"
  }
}

Playground Link: Provided

@MartinJohns
Copy link
Contributor

Duplicate of #9998 and #26914 (you run into both issues).

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jan 13, 2020
@nmain100
Copy link

The very first example given is explained well in #31734. In short, foo is pre-narrowed to null only because typescript saw you assign null to it.

@SQReder
Copy link
Author

SQReder commented Jan 15, 2020

Ok, I've replaced null with undefined, remove the assignment and it begins to work as expected.
Playground

@typescript-bot
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants