Skip to content

this within function typed as any when annotated with JSDoc #51868

Closed

Description

Bug Report

When function is annotated with JSDoc then even if the type explicitly specifies the type of this, the this is typed as any within the function. It seems to work in a *.ts file but not in *.js with JSDoc annotation.

Also it seems to be a regression (or intentional change) in 4.9.4.

🔎 Search Terms

jsdoc this any

🕗 Version & Regression Information

  • This changed between versions 4.8.4 and 4.9.4

⏯ Playground Link

Stackblitz link is below but it doesn't reproduce there as it doesn't seem to support JSDoc well.

  1. Go to https://stackblitz.com/edit/bc4n2m?file=types.d.ts,index.ts,index.js,tsconfig.json&view=editor
  2. Download the project to hard drive
    Screenshot 2022-12-12 at 23 01 15
  3. Install dependencies with npm i or yarn
  4. Open project in VSCode and open file index.js
  5. Hover over this.

💻 Code

index.js

/** @type {import('./types').Module} */
export default function SentryModule () {
  return this.test()
}

types.d.ts

interface ModuleThis {
  foo: () => void;
}

export type Module = (this: ModuleThis) => void;

🙁 Actual behavior

this has any type in TS 4.9.4

🙂 Expected behavior

this is correctly typed as ModuleThis as in TS 4.8.4.

Activity

  1. rchl commented on Dec 13, 2022

    @rchl
    ContributorAuthor

    Note: The current version of VSCode might default to 4.8.4 version in which case the issue will not show up but it reproduces after switching to version of typescript from the project in the bottom right corner of the status bar.

  2. andrewbranch commented on Dec 19, 2022

    @andrewbranch
    Member

    This seems to be an issue specific to typing function declarations. If you switch to a variable declaration initialized to a function expression, it works:

    /** @type {import('./types').Module} */
    export const SentryModule = function() {
      return this.test()
      //          ^^^^ Property 'test' does not exist on type 'ModuleThis'.
    }

    But on the other hand, it’s not as if the annotation on the function declaration does nothing. Mismatched parameters errors as expected:

    /** @type {import('./types').Module} */
    //         ^^^^^^^^^^^^^^^^^^^^^^^^ The type of a function declaration must match the function's signature.
    export default function SentryModule (a, b) {
      return this.test()
    }

    This inconsistency does seem like a bug.

  3. added
    BugA bug in TypeScript
    Effort: ModerateRequires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual".
    checkJsRelates to checking JavaScript using TypeScript
    on Dec 19, 2022
  4. locked as resolved and limited conversation to collaborators on Oct 22, 2025
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

    BugA bug in TypeScriptEffort: ModerateRequires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual".Help WantedYou can do thischeckJsRelates to checking JavaScript using TypeScript

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions