Home > Blockchain >  How can I find the definition of a built-in type like `RegExpExecArray`?
How can I find the definition of a built-in type like `RegExpExecArray`?

Time:11-21

In the following code, the variable x has the type RegExpExecArray | null (according to VSCode).

  const storeIdRegExp = /s(?<storeId>\d )/u;
  const x = storeIdRegExp.exec(query);

How can I find the definition of RegExpExecArray? I couldn't find it documented anywhere.


Note: I did find a couple of references in the TypeScript source code, particularly 1.0lib-noErrors.js and lib.es2018.regexp.d.ts (which I assume applies if the compilation target is ES2018 ). But that wasn't easy to find and I'm not 100% sure the union of these interfaces is the correct answer.

CodePudding user response:

You should write

let x: RegExpExecArray

and then use the "Go to definition" command (ctrl click)

Then the editor will show you all the pieces of its definition:

// lib.es2018.regexp.d.ts
interface RegExpExecArray {
    groups?: {
        [key: string]: string
    }
}
// lib.es2015.d.ts
interface RegExpExecArray extends Array<string> {
    /**
     * The index of the search at which the result was found.
     */
    index: number;
    /**
     * A copy of the search string.
     */
    input: string;
}

CodePudding user response:

Only TypeScript-specific APIs will be documented by TypeScript. The example you referenced are non-TypeScript APIs, therefore there are no official TypeScript documentation.

You will have to find the documentation from its respective sources. For example, RegExp.prototype.exec is a JavaScript API, therefore you should find its documentation from ECMAScript or MDN.

The type references that you mentioned, like RegExpExecArray and many others, are maintained in TypeScript repo, and some other extensions from DefinitelyTyped, but they serve only as references to the official documentation.

  • Related