Deno Style Guide

⚠️ Note that this is the style guide for internal runtime code in the Deno runtime, and in the Deno standard library. This is not meant as a general style guide for users of Deno.

Most modules in the repository should have the following copyright header:

  1. // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.

If the code originates elsewhere, ensure that the file has the proper copyright headers. We only allow MIT, BSD, and Apache licensed code.

Use underscores, not dashes in filenames.

Example: Use file_server.ts instead of file-server.ts.

Add tests for new features.

Each module should contain or be accompanied by tests for its public functionality.

TODO Comments

TODO comments should usually include an issue or the author’s github username in parentheses. Example:

  1. // TODO(ry): Add tests.
  2. // TODO(#123): Support Windows.
  3. // FIXME(#349): Sometimes panics.

Meta-programming is discouraged. Including the use of Proxy.

Be explicit even when it means more code.

There are some situations where it may make sense to use such techniques, but in the vast majority of cases it does not.

Inclusive code

Please follow the guidelines for inclusive code outlined at https://chromium.googlesource.com/chromium/src/+/master/styleguide/inclusive_code.md.

Rust

Follow Rust conventions and be consistent with existing code.

TypeScript

The TypeScript portion of the code base is the standard library std.

Use TypeScript instead of JavaScript.

Use the term “module” instead of “library” or “package”.

For clarity and consistency avoid the terms “library” and “package”. Instead use “module” to refer to a single JS or TS file and also to refer to a directory of TS/JS code.

Do not use the filename index.ts/index.js.

Deno does not treat “index.js” or “index.ts” in a special way. By using these filenames, it suggests that they can be left out of the module specifier when they cannot. This is confusing.

If a directory of code needs a default entry point, use the filename mod.ts. The filename mod.ts follows Rust’s convention, is shorter than index.ts, and doesn’t come with any preconceived notions about how it might work.

Exported functions: max 2 args, put the rest into an options object.

When designing function interfaces, stick to the following rules.

  1. A function that is part of the public API takes 0-2 required arguments, plus (if necessary) an options object (so max 3 total).

  2. Optional parameters should generally go into the options object.

    An optional parameter that’s not in an options object might be acceptable if there is only one, and it seems inconceivable that we would add more optional parameters in the future.

  3. The ‘options’ argument is the only argument that is a regular ‘Object’.

    Other arguments can be objects, but they must be distinguishable from a ‘plain’ Object runtime, by having either:

    • a distinguishing prototype (e.g. Array, Map, Date, class MyThing).
    • a well-known symbol property (e.g. an iterable with Symbol.iterator).

    This allows the API to evolve in a backwards compatible way, even when the position of the options object changes.

```ts, ignore // BAD: optional parameters not part of options object. (#2) export function resolve( hostname: string, family?: “ipv4” | “ipv6”, timeout?: number, ): IPAddress[] {}

  1. ```ts, ignore
  2. // GOOD.
  3. export interface ResolveOptions {
  4. family?: "ipv4" | "ipv6";
  5. timeout?: number;
  6. }
  7. export function resolve(
  8. hostname: string,
  9. options: ResolveOptions = {},
  10. ): IPAddress[] {}

```ts, ignore export interface Environment {

}

// BAD: env could be a regular Object and is therefore indistinguishable // from an options object. (#3) export function runShellWithEnv(cmdline: string, env: Environment): string {}

// GOOD. export interface RunShellOptions { env: Environment; } export function runShellWithEnv( cmdline: string, options: RunShellOptions, ): string {}

  1. ```ts
  2. // BAD: more than 3 arguments (#1), multiple optional parameters (#2).
  3. export function renameSync(
  4. oldname: string,
  5. newname: string,
  6. replaceExisting?: boolean,
  7. followLinks?: boolean,
  8. ) {}
  1. // GOOD.
  2. interface RenameOptions {
  3. replaceExisting?: boolean;
  4. followLinks?: boolean;
  5. }
  6. export function renameSync(
  7. oldname: string,
  8. newname: string,
  9. options: RenameOptions = {},
  10. ) {}
  1. // BAD: too many arguments. (#1)
  2. export function pwrite(
  3. fd: number,
  4. buffer: ArrayBuffer,
  5. offset: number,
  6. length: number,
  7. position: number,
  8. ) {}
  1. // BETTER.
  2. export interface PWrite {
  3. fd: number;
  4. buffer: ArrayBuffer;
  5. offset: number;
  6. length: number;
  7. position: number;
  8. }
  9. export function pwrite(options: PWrite) {}

Export all interfaces that are used as parameters to an exported member

Whenever you are using interfaces that are included in the arguments of an exported member, you should export the interface that is used. Here is an example:

```ts, ignore // my_file.ts export interface Person { name: string; age: number; }

export function createPerson(name: string, age: number): Person { return { name, age }; }

// mod.ts export { createPerson } from “./my_file.ts”; export type { Person } from “./my_file.ts”;

  1. ### Minimize dependencies; do not make circular imports.
  2. Although `std` has no external dependencies, we must still be careful to keep
  3. internal dependencies simple and manageable. In particular, be careful not to
  4. introduce circular imports.
  5. ### If a filename starts with an underscore: `_foo.ts`, do not link to it.
  6. Sometimes there may be situations where an internal module is necessary but its
  7. API is not meant to be stable or linked to. In this case prefix it with an
  8. underscore. By convention, only files in its own directory should import it.
  9. ### Use JSDoc for exported symbols.
  10. We strive for complete documentation. Every exported symbol ideally should have
  11. a documentation line.
  12. If possible, use a single line for the JSDoc. Example:
  13. ```ts
  14. /** foo does bar. */
  15. export function foo() {
  16. // ...
  17. }

It is important that documentation is easily human readable, but there is also a need to provide additional styling information to ensure generated documentation is more rich text. Therefore JSDoc should generally follow markdown markup to enrich the text.

While markdown supports HTML tags, it is forbidden in JSDoc blocks.

Code string literals should be braced with the back-tick (`) instead of quotes. For example:

  1. /** Import something from the `deno` module. */

Do not document function arguments unless they are non-obvious of their intent (though if they are non-obvious intent, the API should be considered anyways). Therefore @param should generally not be used. If @param is used, it should not include the type as TypeScript is already strongly typed.

  1. /**
  2. * Function with non obvious param.
  3. * @param foo Description of non obvious parameter.
  4. */

Vertical spacing should be minimized whenever possible. Therefore single line comments should be written as:

  1. /** This is a good single line JSDoc. */

And not:

  1. /**
  2. * This is a bad single line JSDoc.
  3. */

Code examples should utilize markdown format, like so:

  1. /** A straight forward comment and an example:
  2. * ```ts
  3. * import { foo } from "deno";
  4. * foo("bar");
  5. * ```
  6. */

Code examples should not contain additional comments and must not be indented. It is already inside a comment. If it needs further comments it is not a good example.

Resolve linting problems using directives

Currently, the building process uses dlint to validate linting problems in the code. If the task requires code that is non-conformant to linter use deno-lint-ignore <code> directive to suppress the warning.

  1. // deno-lint-ignore no-explicit-any
  2. let x: any;

This ensures the continuous integration process doesn’t fail due to linting problems, but it should be used scarcely.

Each module should come with a test module.

Every module with public functionality foo.ts should come with a test module foo_test.ts. A test for a std module should go in std/tests due to their different contexts, otherwise it should just be a sibling to the tested module.

Unit Tests should be explicit.

For a better understanding of the tests, function should be correctly named as its prompted throughout the test command. Like:

  1. test myTestFunction ... ok

Example of test:

```ts, ignore import { assertEquals } from “https://deno.land/std@$STD_VERSION/testing/asserts.ts“; import { foo } from “./mod.ts”;

Deno.test(“myTestFunction”, function () { assertEquals(foo(), { bar: “bar” }); });

  1. ### Top level functions should not use arrow syntax.
  2. Top level functions should use the `function` keyword. Arrow syntax should be
  3. limited to closures.
  4. Bad:
  5. ```ts
  6. export const foo = (): string => {
  7. return "bar";
  8. };

Good:

  1. export function foo(): string {
  2. return "bar";
  3. }

std

Do not depend on external code.

https://deno.land/std/ is intended to be baseline functionality that all Deno programs can rely on. We want to guarantee to users that this code does not include potentially unreviewed third party code.

Document and maintain browser compatibility.

If a module is browser compatible, include the following in the JSDoc at the top of the module:

  1. // This module is browser compatible.

Maintain browser compatibility for such a module by either not using the global Deno namespace or feature-testing for it. Make sure any new dependencies are also browser compatible.