denoland / doc_website

Archived. New version at https://github.com/denoland/docland
https://doc.deno.land/
MIT License
194 stars 42 forks source link

feat: support user-defined type guards as return type #210

Closed magurotuna closed 3 years ago

magurotuna commented 3 years ago

This PR adds support for user-defined type guards as return type. Fixes #207 Fixes https://github.com/denoland/deno_doc/issues/102

It depends on deno_doc v0.6.0+, so it doesn't work with the current latest version of Deno (1.11.2). To see it working locally, you have to specify the path to the canary or a binary you built on your own from the latest source. In my case, I changed api/docs.ts L27-32 to:

  const proc = Deno.run({
    cmd: ["/Users/yusuktan/Repos/github.com/magurotuna/deno/target/release/deno", "doc", sourceFile, "--json", "--reload"],
    stdout: "piped",
    stderr: "piped",
    env: { "DENO_DIR": "/tmp/denodir" },
  });

then I ran vercel dev and attempted to generate doc for the following snippet.

export function f1(val1: A | B): val1 is A {}
export function f2(val2: any): asserts val2 is string {}
export function f3(val3: any): asserts val3 {}
export function assertIsDefined<T>(val4: T): asserts val4 is NonNullable<T> {
  if (val === undefined || val === null) {
    throw new AssertionError(
      `Expected 'val' to be defined, but received ${val}`
    );
  }
}
export class C {
  isSomething(): this is Something {
    return this instanceof Something;
  }
}

export type A = string | number;

Finally, the generated doc looks like:

image

Ref: https://github.com/denoland/deno_doc/pull/105

CLAassistant commented 3 years ago

CLA assistant check
All committers have signed the CLA.

magurotuna commented 3 years ago

Probably this is ready because of #204

Preview: https://doc-website-814azk249-denoland.vercel.app/https/gist.githubusercontent.com%2Fmagurotuna%2Fa35b83de6e70c53b9d5322fc2d51addd%2Fraw%2F619da3b332d5e5a42f3cfafd2c12310817f7754e%2Fuser_defined_type_guard.ts