Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Coding guidelines

Eli Barzilay edited this pageMay 24, 2021 ·19 revisions

STOP READING IMMEDIATELY

THIS PAGE PROBABLY DOESNOT PERTAIN TO YOU.

These are Coding Guidelines forContributors to TypeScript.This isNOT a prescriptive guideline for the TypeScript community.These guidelines are meant forcontributors to the TypeScript project's codebase.We have chosen many of them for team consistency. Feel free to adopt them for your own team.

AGAIN: This isNOT a prescriptive guideline for the TypeScript community

Please do not file issues about these guidelines.

Names

  1. Use PascalCase for type names.
  2. Do not useI as a prefix for interface names.
  3. Use PascalCase for enum values.
  4. Use camelCase for function names.
  5. Use camelCase for property names and local variables.
  6. Do not use_ as a prefix for private properties.
  7. Use whole words in names when possible.

Components

  1. 1 file per logical component (e.g. parser, scanner, emitter, checker).
  2. Do not add new files. :)
  3. files with.generated.* suffix are auto-generated, do not hand-edit them.

Types

  1. Do not export types/functions unless you need to share it across multiple components.
  2. Do not introduce new types/values to the global namespace.
  3. Shared types should be defined intypes.ts.
  4. Within a file, type definitions should come first.

null andundefined

  1. Useundefined. Do not use null.

General Assumptions

  1. Consider objects like Nodes, Symbols, etc. as immutable outside the component that created them. Do not change them.
  2. Consider arrays as immutable by default after creation.

Classes

  1. For consistency, do not use classes in the core compiler pipeline. Use function closures instead.

Flags

  1. More than 2 related Boolean properties on a type should be turned into a flag.

Comments

  1. Use JSDoc style comments for functions, interfaces, enums, and classes.

Strings

  1. Use double quotes for strings.
  2. All strings visible to the user need to be localized (make an entry in diagnosticMessages.json).

Diagnostic Messages

  1. Use a period at the end of a sentence.
  2. Use indefinite articles for indefinite entities.
  3. Definite entities should be named (this is for a variable name, type name, etc..).
  4. When stating a rule, the subject should be in the singular (e.g. "An external module cannot..." instead of "External modules cannot...").
  5. Use present tense.

Diagnostic Message Codes

Diagnostics are categorized into general ranges. If adding a new diagnostic message, use the first integral number greater than the last used number in the appropriate range.

  • 1000 range for syntactic messages
  • 2000 for semantic messages
  • 4000 for declaration emit messages
  • 5000 for compiler options messages
  • 6000 for command line compiler messages
  • 7000 for noImplicitAny messages

General Constructs

For a variety of reasons, we avoid certain constructs, and use some of our own. Among them:

  1. Do not usefor..in statements; instead, usets.forEach,ts.forEachKey andts.forEachValue. Be aware of their slightly different semantics.
  2. Try to usets.forEach,ts.map, andts.filter instead of loops when it is not strongly inconvenient.

Style

  1. Use arrow functions over anonymous function expressions.
  2. Only surround arrow function parameters when necessary.
    For example,(x) => x + x is wrong but the following are correct:
    • x => x + x
    • (x,y) => x + y
    • <T>(x: T, y: T) => x === y
  3. Always surround loop and conditional bodies with curly braces. Statements on the same line are allowed to omit braces.
  4. Open curly braces always go on the same line as whatever necessitates them.
  5. Parenthesized constructs should have no surrounding whitespace.
    A single space follows commas, colons, and semicolons in those constructs. For example:
    • for (var i = 0, n = str.length; i < 10; i++) { }
    • if (x < 10) { }
    • function f(x: number, y: string): void { }
  6. Use a single declaration per variable statement
    (i.e. usevar x = 1; var y = 2; overvar x = 1, y = 2;).
  7. else goes on a separate line from the closing curly brace.
  8. Use 4 spaces per indentation.

Want to contribute to this Wiki?

Fork it and send a pull request.

News

Debugging TypeScript

Contributing to TypeScript

Building Tools for TypeScript

FAQs

The Main Repo

Clone this wiki locally

[8]ページ先頭

©2009-2025 Movatter.jp