microsoft / microsoft/TypeScript

Issues with JSDoc private and protected

Open
#42,237 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Report

🔎 Search Terms

JSDoc access modifier

🕗 Version & Regression Information
  • This changed in version v3.8 probably because JSDoc accessibility modifiers were introduced; that is to say: it never worked
⏯ Playground Link

Playground Link: Provided

💻 Code
/** @class */
function Legacy () {
  /** @protected */
  this.prop = 1; // should not be an error
  this.doStuff(); // should not be an error
}

/** @private */
Legacy.prototype.doStuff = function () {
  this.prop; // should not be an error
  this.doStuff(); // should not be an error
}

class C {
  /** @protected */
  prop = 1;
}

/** @this {C} */
function fn() {
  this.prop; // the equivalent works in TS
}

Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "alwaysStrict": true,
    "esModuleInterop": true,
    "checkJs": true,
    "allowJs": true,
    "declaration": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "moduleResolution": 2,
    "target": "ES2017",
    "jsx": "React",
    "module": "ESNext"
  }
}
🙁 Actual behavior

Using legacy class syntax and so-called "assignment declarations" don't recognize that the properties are actually accessed inside the class. That's because the checker logic only looks for real ClassLikeDeclaration Nodes.

Note that @readonly correctly recognizes function Legacy as the constructor and allows assignment there.

Using a protected member in a function with explicit this-parameter works in TS, but doesn't when using @this in JS.

🙂 Expected behavior

No errors in the code.

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

Start with the provided TypeScript Playground reproduction and compare the legacy constructor, assignment declarations, and @this cases under checkJs. Trace the checker behavior described in the report, including the contrasting @readonly behavior. Done means the supplied code produces no errors while preserving the expected protected and private access checks.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.