microsoft / microsoft/TypeScript

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

Open
#42,175 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: This-Typing
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the contextual this examples in the issue with TypeScript 4.2.0-dev.20210101 and the listed compiler options, using the provided Playground link as the starting point. Trace how assertion signatures and never-returning calls affect narrowing and reachability, then add regression coverage showing the expected diagnostic or narrowing behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.