microsoft / microsoft/TypeScript

Checking JS types does not accept @implements or @augments on constructor functions

Open
#38,985 2 comments 19 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 3.9.5 (also tested on 3.9.3 initially)

Search Terms: @implements, jsdoc

Code

/**
 * @param {string} name
 * @implements {EventTarget}
 * @constructor
 */
function Foo (name) {
  this.name = name
}

/**
 * @param {string} type
 * @param {EventListenerOrEventListenerObject} listener
 * @param {boolean | AddEventListenerOptions} [options]
 */
Foo.prototype.addEventListener = function (type, listener, options) {}

Then run tsc --noEmit --checkJS --allowJS reproducer.js

Expected behavior:

According to https://www.typescriptlang.org/docs/handbook/type-checking-javascript-files.html#constructor-functions-are-equivalent-to-classes, constructor functions are treated like classes. So using @implements or @augments on them should be possible. It used to work in the past.

Actual behavior:

reproducer.js:6:10 - error TS8022: JSDoc '@implements' is not attached to a class.

6 function Foo (name) {
           ~~~


Found 1 error.

Playground Link: I was not able to reproduce this in the playground (but it does not have the latest 3.9 versions available)

Related Issues:

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 reproducer.js example and run tsc --noEmit --checkJS --allowJS reproducer.js using the affected TypeScript version. Trace the handling of JSDoc @implements and @augments on constructor functions; done means the annotations are accepted without TS8022 and the existing JavaScript checks remain valid.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.