deno_doc icon indicating copy to clipboard operation
deno_doc copied to clipboard

`deno doc` doesn't infer types from object literal

Open uki00a opened this issue 5 years ago • 4 comments

Example

https://deno.land/x/[email protected]/protocol/reply.ts contains the following definition:

export const replyTypes = {
  Integer: "integer",
  SimpleString: "simple string",
  Array: "array",
  BulkString: "bulk string",
} as const;

deno doc generates the following output for the above file:

$ deno doc https://deno.land/x/[email protected]/protocol/reply.ts replyTypes

Defined in https://deno.land/x/[email protected]/protocol/reply.ts:12:0

const replyTypes

I think deno doc should output type of the replyTypes variable, but outputs nothing.

Environment

  • deno v1.8.2

uki00a avatar Mar 27 '21 03:03 uki00a

This isn't a defect, it is either a current design limitation and/or feature request, as we do not type inference (e.g. inferred return types, or in this case, object literals. We only document specified type annotations.

kitsonk avatar Sep 02 '21 11:09 kitsonk

Correction, we do infer some trivial types for const variables, just not more complex types like object literals.

kitsonk avatar Sep 08 '21 04:09 kitsonk

As mentioned in #113, we should also ensure that when we infer types for an object literal, we infer the associated JSDoc block.

kitsonk avatar Sep 10 '21 04:09 kitsonk

Posting another example from Deno source.

JSDoc is stripped for methods in object literals.

Type Definitions for Deno.env from lib.deno.ns.d.ts

  export const env: {
    /** Retrieve the value of an environment variable. Returns `undefined` if that
     * key doesn't exist.
     *
     * ```ts
     * console.log(Deno.env.get("HOME"));  // e.g. outputs "/home/alice"
     * console.log(Deno.env.get("MADE_UP_VAR"));  // outputs "undefined"
     * ```
     * Requires `allow-env` permission. */
    get(key: string): string | undefined;

    /** Set the value of an environment variable.
     *
     * ```ts
     * Deno.env.set("SOME_VAR", "Value");
     * Deno.env.get("SOME_VAR");  // outputs "Value"
     * ```
     *
     * Requires `allow-env` permission. */
    set(key: string, value: string): void;

    /** Delete the value of an environment variable.
     *
     * ```ts
     * Deno.env.set("SOME_VAR", "Value");
     * Deno.env.delete("SOME_VAR");  // outputs "undefined"
     * ```
     *
     * Requires `allow-env` permission. */
    delete(key: string): void;

    /** Returns a snapshot of the environment variables at invocation.
     *
     * ```ts
     * Deno.env.set("TEST_VAR", "A");
     * const myEnv = Deno.env.toObject();
     * console.log(myEnv.SHELL);
     * Deno.env.set("TEST_VAR", "B");
     * console.log(myEnv.TEST_VAR);  // outputs "A"
     * ```
     *
     * Requires `allow-env` permission. */
    toObject(): { [index: string]: string };
  };

Rendered:

CleanShot 2021-10-08 at 09 11 41@2x

satyarohith avatar Oct 08 '21 03:10 satyarohith