rushstack icon indicating copy to clipboard operation
rushstack copied to clipboard

[api-extractor] how to doc this

Open Jeffy2012 opened this issue 7 years ago • 5 comments

Is this a feature or a bug?

  • [X] Feature
  • [ ] Bug

Please describe the actual behavior.

no api

import { Random } from 'some-module';

declare module 'some-module' {
  export interface Person {
    /**
     *
     * @public
     */
    url(): string;
  }
}

/**
 * 返回一个url
 * @public
 */
Person.prototype.url = function() {
  return '123';
};

Jeffy2012 avatar Aug 22 '19 17:08 Jeffy2012

API Extractor doesn't support declare module style declarations. Typically these are legacy JavaScript APIs that already have JavaScript-based documentation.

It is possible to work around this limitation by creating a special entry point used by API Extractor, that exports those declarations as modern exports. Whereas consumers of your library would import them from the normal entry point.

Or, if this is not a legacy project: You could change your code to use modern conventions, something like this:

some-module/package.json

{
  "name": "some-module",
  "version": "1.0.0",
  "main": "lib/index.js",
  "typings": "lib/index.d.ts"
}

some-module/lib/index.d.ts

/** @public */
export interface Person {
  /** The URL */
  url(): string;
}

/** @public */
export namespace Person {
  /**
   * 返回一个url
   */
  export function url(): string {
    return '123';
  }
}

Besides being modern, this gives other benefits such as the ability to load two different versions of your package side-by-side.

octogonz avatar Aug 22 '19 23:08 octogonz

I want split a big class into multiple files?

How?

Jeffy2012 avatar Aug 23 '19 04:08 Jeffy2012

TypeScript doesn't provide anything like partial classes from C#. The reason is that TypeScript tries to add static type checking to ECMAScript without substantially altering the underlying language. In other words, after deleting all the type annotations, your TypeScript source code should more-or-less be directly executable by a JavaScript runtime. Thus the compiler is unlikely to implement this feature unless ECMAScript decides to support it.

In order from "best" to "least preferable", here's how I would split up the files:

  1. Don't design huge classes; instead break up your code into smaller components :-)
  2. Use base classes; Separate your functionality into a hierarchy of base classes. This often improves organization by (1) restricting what-depends-on-what, and (2) isolating state in private members.
  3. Extract private members into external helpers; often much of the implementation can be nicely modeled as pure functions (that doesn't need to access private members), which cuts down code size. Then your original class becomes mostly the public API.
  4. Just allow the source file to be really big; although it's not the best practice, having thousands of lines in one file will be processed just fine by the compiler and JIT. A large file may be acceptable in some cases, especially for legacy code.

Personally, I would discourage the approaches below:

  • Use module augmentation or declaration merging.
  • Reassign class members:
    import func from 'other-file';
    class X { 
      public member = func;
    } 
    
  • Reassign prototypes:
    import func from 'other-file';
    class X { }
    X.prototype.member = func;
    

These approaches are not stereotypical TypeScript code. In my opinion they're less readable, and may be confusing for consumers of your API. They're awkward to document.

This is just a personal opinion, though. Perhaps other people have a different viewpoint.

octogonz avatar Aug 23 '19 17:08 octogonz

Closing due to inactivity.

octogonz avatar Jul 02 '20 22:07 octogonz

API Extractor doesn't support declare module style declarations. Typically these are legacy JavaScript APIs that already have JavaScript-based documentation.

sometimes we use declare module to extend module's interface. Like Vue.js, our code:

// a.ts
export declare interface Person {
  name: string;
}

// b.ts
import './a.ts';
declare module './a.ts' {
  interface Person {
    age: number;
  }
}

I though this is the only way to extend some module while ae is unsupported. Is there other way to extend a module?###

DennisJames avatar Jul 06 '20 02:07 DennisJames