question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for JSDoc interface definition

See original GitHub issue

Search Terms

jsdoc, interface I found https://github.com/microsoft/TypeScript/issues/16142 which was closed by the reporter, and never reopened even though some other people suggested it.

Suggestion

JSDoc has a way to declare interfaces, through @interface, @function (and then @implements on the class). It would be great if tsc could support them (defining a TS interface based on that info).

Use Cases

JSDoc interfaces are useful to define interfaces implemented by multiple classes, that are then used based on this interface. This is part of JSDoc, but currently unsupported by tsc.

Examples

/**
 * @interface Metric
 */
/**
 * @function
 * @name Metric#getName
 * @returns {string}
 */
/**
 * @function
 * @name Metric#compute
 * @param context
 * @param [extra]
 * @returns {Promise<object>}
 */

/**
 * @constructor
 * @implements {Metric}
 */
function ActivityScoreMetric () {
    // [REDACTED]
}
ActivityScoreMetric.prototype = {
  compute: function (context, extra) {
      // [REDACTED]
  },

  getName: function () {
    return 'activity_score'
  }
}

// Other implementations go there

// Later code deals with `Array<Metric>` for some variables

Checklist

My suggestion meets these guidelines:

  • This wouldn’t be a breaking change in existing TypeScript/JavaScript code
  • This wouldn’t change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript’s Design Goals.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:7
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
sandersncommented, Sep 3, 2019

This is a sub-issue of https://github.com/microsoft/TypeScript/issues/30624. Right now we have decided not to make all of Typescript’s features available in JSDoc. Writing 40 lines of metadata in a comment is not much different from putting those same lines in a d.ts file, and we believe the d.ts syntax is much more readable to humans.

1reaction
TimvdLippecommented, Dec 10, 2019

We use @interface extensively in Chrome DevTools and the lack of support with checkJs means we can not use TypeScript to check our JavaScript. Our usage: https://cs.chromium.org/search/?q=f:devtools-frontend+@interface&sq=package:chromium&type=cs

I am not sure if we can use the .d.ts trick, as TS itself complains about how classes annotated with @interface are defined (it complains about missing return statements for example)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use JSDoc: @interface
The @interface tag marks a symbol as an interface that other symbols can implement. For example, your code might define a parent class...
Read more >
How to Write TypeScript Interfaces in JSDoc Comments
Thankfully TypeScript supports type-checking .js files via JSDoc comments. But how do you write out interfaces without a .ts file? Glad you ...
Read more >
JSDoc Reference - TypeScript: Documentation
The list below outlines which constructs are currently supported when using JSDoc annotations to provide type information in JavaScript files.
Read more >
TypeScript jsdoc for interface properties - Stack Overflow
I would like to use JSDoc to document this interface to help with auto-complete in vscode. Here's the current interface: export interface ......
Read more >
ES6 implementation of JSDoc defined interface : WEB-19755
Implementation of methods defined in an interface via JSDoc produces ES5 style implementation and does not recognize ES6 implementations. The ES5 implementation ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found