Skip to content

Assertion signature on contextual 'this'-parameter doesn't narrow and has no error #42175

Description

TypeScript Version: 4.2.0-dev.20210101

Search Terms: asserts this

Expected behavior:

Consistent behavior. Either

  • this.assert(v) narrows v
  • this.assert has an error about missing type annotation, no narrowing, this.returnNever() should not mark the following code as unreachable

Actual behavior:

No narrowing from asserts, no error on call expression, never-returning function has control flow effect.

Code

declare function makeContextualThisParameter<T>(cb: (this: T) => any): void;

makeContextualThisParameter<{assert(v: any): asserts v}>(function() {
    const v = Boolean();
    this.assert(v); // expected error here
    v; // or narrowing `v: true` here
});

makeContextualThisParameter<{returnNever(): never}>(function() {
    this.returnNever();
    debugger; // this is marked as unreachable
});
Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "alwaysStrict": true,
    "esModuleInterop": true,
    "declaration": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "moduleResolution": 2,
    "target": "ES2017",
    "jsx": "React",
    "module": "ESNext"
  }
}

Playground Link: Provided

Activity

  1. added
    Domain: This-Typing
    on Oct 16, 2025
  2. added
    Domain: check: Order Dependence
    on Nov 18, 2026
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

    BugDomain: This-TypingDomain: check: Order Dependence

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions