Skip to content

Support @extends tag for ES5-style classes #36369

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

Open
kayahr opened this issue Jan 23, 2020 · 5 comments
Open

Support @extends tag for ES5-style classes #36369

kayahr opened this issue Jan 23, 2020 · 5 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@kayahr
Copy link

kayahr commented Jan 23, 2020

TypeScript Version: 3.7.5 and 3.8.0-dev.20200123

Search Terms: extends not attached to a class

I'm currently evaluating if TypeScript can be used to type-check old closure compiler projects which use JSDoc annotations for typing and to generate *.d.ts files for it so the old projects can be easily used within new typescript projects. But unfortunately the @extends annotation is not working as expected. I would understand if TypeScript simply didn't support it (Then this would be a feature request) but the error message error TS8022: JSDoc '@extends' is not attached to a class thrown by the compiler suggests that there is some support for but it doesn't work as expected.

Please note that the real class inheritance code which actually extends class Sub from Base is omitted in the code example below because it is irrelevant to the compiler.

Code

// Compile with: tsc --checkJS --allowJS --outDir out test.js

/**
 * @constructor
 * @class
 */
function Base() {}

Base.prototype.foo = function() {}

/**
 * @constructor
 * @class
 * @extends {Base}
 */
function Sub() {}

Sub.prototype.bar = function() {}

Expected behavior:

Typescript should recognize type Sub to be a class which extends class Base.

Actual behavior:

Compilation fails with this error:

$ tsc --checkJS --allowJS --outDir out test.js
test.js:14:10 - error TS8022: JSDoc '@extends' is not attached to a class.

14 function Sub() {}
            ~~~


Found 1 error.
@dragomirtitian
Copy link
Contributor

I think (having recently looked at it) @extends only supports classes declared with class not old-style es5 classes.

@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Jan 27, 2020
@RyanCavanaugh RyanCavanaugh changed the title Error: JSDoc '@extends' is not attached to a class Support @extends tag for ES5-style classes Jan 27, 2020
@jpoehnelt
Copy link

I'm currently evaluating if TypeScript can be used to type-check old closure compiler projects which use JSDoc annotations for typing and to generate *.d.ts files for it so the old projects can be easily used within new typescript projects.

I have this exact use case. Anyone done any digging as to how difficult this would be to support? I have no experience with TypeScript internals.

@sandersn
Copy link
Member

sandersn commented Jul 6, 2020

#13206 is the main issue tracking inheritance for ES5 constructor functions.

@jpoehnelt Full support is not hard, but would be tricky since it messes with the core class/this instantiation code. The code would be fairly similar to support for @class; create a getEffectiveExtendsClause that checks extends AND @extends, then make sure that it's used everywhere. There are likely quite a few places that need to be updated.

@ljharb
Copy link
Contributor

ljharb commented Jan 26, 2024

I have this use case as well - I have a constructor function Foo (that can't be turned into class syntax) that has util.inherits(Foo, EventEmitter);, and I have no way to tell TS that the constructor extends EventEmitter, with jsdoc or a d.ts.

@bamtang-dev
Copy link

+1 please fix this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

7 participants