forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Coding guidelines
Daniel Rosenwasser edited this page Nov 27, 2015
·
6 revisions
NOTE: These are Coding Guidelines for TypeScript Contributors
While these guidelines are mainly meant for contributors to the TypeScript project, certain suggestions apply as to how idiomatic TypeScript code should be written (e.g. the "Names" and "Style" sections).
- Use PascalCase for type names.
- Do not use "I" as a prefix for interface names.
- Use PascalCase for enum values.
- Use camelCase for function names.
- Use camelCase for property names and local variables.
- Do not use "_" as a prefix for private properties.
- Use whole words in names when possible.
- 1 file per logical component (e.g. parser, scanner, emitter, checker).
- Do not add new files. :)
- files with ".generated.*" suffix are auto-generated, do not hand-edit them.
- Do not export types/functions unless you need to share it across multiple components.
- Do not introduce new types/values to the global namespace.
- Shared types should be defined in 'types.ts'.
- Within a file, type definitions should come first.
- Use undefined, do not use null.
- Consider objects like Nodes, Symbols, etc. as immutable outside the component that created them. Do not change them.
- Consider arrays as immutable by default after creation.
- For consistency, do not use classes in the core compiler pipeline. Use function closures instead.
- More than 2 related Boolean properties on a type should be turned into a flag.
- Use JSDoc style comments for functions, interfaces, enums, and classes.
- Use double quotes for strings.
- All strings visible to the user need to be localized (make an entry in diagnosticMessages.json).
- Use a period at the end of a sentence.
- Use indefinite articles for indefinite entities.
- Definite entities should be named (this is for a variable name, type name, etc..).
- When stating a rule, the subject should be in the singular (e.g. "An external module cannot..." instead of "External modules cannot...").
- Use present tense.
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
For a variety of reasons, we avoid certain constructs, and use some of our own. Among them:
- Do not use ECMAScript 5 functions; instead use those found in core.ts.
- Do not use
for..in
statements; instead, usets.forEach
,ts.forEachKey
andts.forEachValue
. Be aware of their slightly different semantics. - Try to use
ts.forEach
,ts.map
, andts.filter
instead of loops when it is not strongly inconvenient.
- Use arrow functions over anonymous function expressions.
- 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
- Always surround loop and conditional bodies with curly braces.
- Open curly braces always go on the same line as whatever necessitates them.
- 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 { }
- Use a single declaration per variable statement
(i.e. usevar x = 1; var y = 2;
overvar x = 1, y = 2;
). -
else
goes on a separate line from the closing curly brace.
TypeScript Language Basics
- Basic Types
- Interfaces
- Classes
- Namespaces and Modules
- Functions
- Generics
- Common Errors
- Integrating with Build Tools
- Compiler Options
- tsconfig.json
TypeScript Language Advanced
- Mixins
- Declaration Merging
- Type Inference
- Type Compatibility
- JSX
- Writing Definition Files
- Typings for NPM packages
News
TypeScript Contributors
- Contributing to TypeScript
- TypeScript Design Goals
- Coding Guidelines
- Spec conformance testing
- Useful Links for TypeScript Issue Management
- Writing Good Design Proposals
- Compiler Internals
Building Tools for TypeScript
- Architectural Overview
- Using the Compiler API
- Using the Language Service API
- Dev Mode in Visual Studio
FAQs