简体   繁体   中英

how to refer to a private class field in typescript jsdoc

Please tell me: how to refer to a private class field in typescript jsdoc?

script.js:

// @ts-check

class C {

    /** @type { null | string | number } */
    #x = null;

    // /** @type { ( x : C[ 'x' ] ) => void } */
    // /** @type { ( x : this[ 'x' ] ) => void } */
    // /** @type { ( x : this[ '#x' ] ) => void } */
    // /** @type { ( x : this[ #x ] ) => void } */
    // /** @type { ( x : this#x ) => void } */
    // /** @type { ( x : this.#x ) => void } */
    // /** @type { ( x : this[ #'x' ] ) => void } */
    f( x ) { };

};

This is an open issue in Typescript itself. But there is a workaround to get what you want.

class C {
  #x: null | string | number = null;

  f = (() => {
    const privX = this.#x
    return (x: typeof privX) => {
    }
  })();
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM