Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
zombie opened this issue May 29, 2018 · 4 comments
Closed

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

zombie opened this issue May 29, 2018 · 4 comments
Assignees
Labels
Bug A bug in TypeScript checkJs Relates to checking JavaScript using TypeScript Domain: JSDoc Relates to JSDoc parsing and type generation

Comments

@zombie
Copy link

zombie commented May 29, 2018

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.
@mhegazy mhegazy added the Bug A bug in TypeScript label May 29, 2018
@mhegazy mhegazy added this to the TypeScript 3.0 milestone May 29, 2018
@mhegazy mhegazy added Domain: JSDoc Relates to JSDoc parsing and type generation checkJs Relates to checking JavaScript using TypeScript labels May 29, 2018
@mhegazy
Copy link
Contributor

mhegazy commented May 29, 2018

I think this is related to #22896, and #24467. I would say my proposal in #22896 (comment) should address the underlying use case.

@zombie
Copy link
Author

zombie commented May 30, 2018

Yes, that indeed sounds like a better solution, as long as it covers the above use case and doesn't throw "Class 'ExtensionAPI' defines instance member property 'startup', but extended class 'Linter' defines it as instance member function." (I'm not even sure when that error would be useful).

Hopefully that issue gets a milestone soon.

@mhegazy mhegazy modified the milestones: TypeScript 3.0, TypeScript 3.1 Jul 2, 2018
@sandersn
Copy link
Member

@zombie Can you try this on 3.0 or newer? I don't get the error there.

@zombie
Copy link
Author

zombie commented Aug 24, 2018

Doesn't error anymore, thanks (and still looking forward to @property).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript checkJs Relates to checking JavaScript using TypeScript Domain: JSDoc Relates to JSDoc parsing and type generation
Projects
None yet
Development

No branches or pull requests

3 participants