Skip to content

JSDoc: unable to define an optional method on base class #24470

Closed
@zombie

Description

@zombie

TypeScript Version: 2.8.3

We have a base "abstract" (this is plain JS) class that defines an ExtensionAPI which extensions need to extend to provide the implementation. Some of the methods (like startup) are optional, and if not present, we can delay initializing the extensions on startup.

Typescript check complains that the base class doesn't have such methods, and the best solution I could figure out is to add a property to the ExtensionAPI.prototype and properly @type it, but that throws with error TS2425.

Code

class ExtensionAPI {
  run(file) {
    //
  }
}

/** @type {function(string): void} */
ExtensionAPI.prototype.startup = null;

///

class Linter extends ExtensionAPI {
  startup(file) {
    //
  }
  run(file) {
    //
  }
}

///

/** 
 * @param {ExtensionAPI[]} extensions
 */
function init(extensions, file) {
  for (let ext of extensions) {
    if (ext.startup) {
      ext.startup(file);
    }
  }  
}

init([new Linter()], "Utitled1");

Typecheck using:

tsc --allowjs --checkjs --noemit extensions.js

Actual behavior:

error TS2425: Class 'ExtensionAPI' defines instance member property 'startup', but extended class 'Linter' defines it as instance member function.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDomain: JSDocRelates to JSDoc parsing and type generationcheckJsRelates to checking JavaScript using TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions