-
Use TypeScript language during development
-
All local variables, function parameters should be named in
camelCasestyle, without any special prefixes -
Specify
local variables,parametersandreturn valuetypes:
function add(firstParam: number, secondParam: number) : number {
const localVariable: number = firstParam + secondParam;
return localVariable;
}-
All functions should be named in a
camelCasestyle -
Use
constfor local variables that woudn't changes:
function max(values: number[]) : number {
let maxValue = values[0];
for (let i = 0; i < values.length; ++i) {
const v = values[i];
if (v > maxValue)
maxValue = v;
}
return maxValue;
}- Prevent importing all from module. Where it possible use:
import { Class1, Class2 } from './classes';-
Don't miss
;symbol at the end of expressions -
Throw exceptions when it goes to wrong logic behavior. Use
throw 'string'to throw any exception:
if (...)
throw "Undefined behavior"; // format string as you wish (but include some useful info about error)- All class members (not functions) should start with
_prefix. And has acamelCasestyle name:
class ExampleClass {
private _exampleMember: number;
}- Preffer to use
setter/getterfunctionality (seeAccessorssection at classes documentation)
class ExampleClass {
private _value: string;
get value() : string {
return this._value;
}
set value(newValue: string) {
this._value = newValue;
}
}- Name methods with a
camelCasestyle:
class MyClass {
myMethod() : void {
}
}-
Name classes in style
ClassName -
Use
private,protected,publickeywords to control methods and members visibility -
Use
exportkeyword with class definition when export it from module:
export class MyExportedClass {
}-
Do not export classes from module, if they use only in the last one
-
For arguments in methods, that has the same name as properties use
_argNamenames. In example, if we will usexname for argument in constructor, then it would be undefined behavior, because we havexgetter. So use_xargument name:
class Vector {
private _x;
private _y;
constructor(_x: number, _y: number) {
this._x = _x;
this._y = _y;
}
get x(): number {
return this._x;
}
}