Skip to content

Latest commit

 

History

History
240 lines (194 loc) · 10.1 KB

README.md

File metadata and controls

240 lines (194 loc) · 10.1 KB

Naming-Conventions

These are some rules for my TypeScript projects.

Directory

Filename

All filenames are written in snakecase style. In the file name, letters from lower case a-z and _ are allowed. Use numbers 0-9 only if it is necessary. For example, if the function is called encodeBase64.

Example

The filename would look like this:

encode_base64.ts

Foldername

In the folder name, letters from lower case a-z and _ are allowed. Never use numbers to sort folders. For example, 01_help, 02_error, 03_components, ...

Filecontent

A file should have only one main function/class which is exported. The function/class should then be named exactly like the file name, in camelcase. If you have an object that mimics an enum then the associated type of this object will be exported as well. You can read more about this in the enum section.

Example

For example, if we want to write a function that reads a text file, we would name the file read_text_file.ts. In the file we would then export a function which looks like this.

export default function readTextFile() : string {
  //...
}

Function name readTextFile() becomes Filename read_text_file.ts.

Helpers/Untilities

Helper functions/objects for the main function that gets exportet are allowed. It is also allowed to export an object and a type as long as the type is created from the object. You can look up the reason for this under enum.

Vertical character limit

A file may contain a maximum of 30 lines of code (excluding comments and whitespace between lines). This is for increasing readability. If the number of lines or the number of indentations is exceeded, then the code must be refactored to separate files. This rule does not apply to data structures as for example json files.

Horizontal character limit

The maximum character limit for a line is 120 characters and a maximum of 4 indentations.


Documentation/Comments

All TypeScript files are documented in the JSDoc format.


Variables/Types

All variables are written in camelcase. You can read more about that in the let/const/var section.

Example
const greeting = 'Hello World';
let age : number;

Spaces are inserted between a variable and the colon and between the colon and a type.

Example
let word : string;

When a value is assigned to a variable in an object, no space is inserted between the variable and the colon. This is to distinguish a direct type assignment from a value assignment.

Example
const object = {key: 'Text'};

Declaration-Statements

let/const/var

let/const names are written in camelcase. The same is applied to members of objects. Avoid the use of var, use let/const instead.

Example
const messageVariants = {greetingMessage: 'Hey, nice to see you.'}

If the object has more than one key or the key name exceeds the maximum character limit for a line then the line needs to be wrapped.

Example
const messageVariants = {
  greetingMessage: 'Hey, nice to see you.',
  goodbyeMessage: 'Bye!'
}

If you declare a variable whose content does not change, then this variable is written in ALL_CAPS. This rule does not apply to objects described in other structures, like "enum like objects" in the enum section.

Example
const API_ENDPOINT = 'https://api.example.com';

interface

Interface names are started with a capital letter. The rest of the name is written in camelcase. Also known as PascalCase. Variable definitions are separated by a comma. Each key in an interface needs to be on a separate line.

Example
interface FooBar {
  text : string,
  value : number
}

type

...

enum

Enum names are written in camelcase. Enum variables are written in all caps. Each key in an enum needs to be on a separate line.

Example
enum textTypes {
  TEXT_A,
  TEXT_B
}

Avoid using enums as presets for default values. Use enum like objects instead. Always export these objects as const.

Example
export const textTypes = {
  TEXT_A: 0,
  TEXT_B: 1
} as const

export typeof TextType[keyof typeof textTypes]

function

Function names are written in camelcase. If the function has more than one parameter or the parameter name exceeds the maximum character limit for a line then the line needs to be wrapped. The same rule also applies to the content of the function. Always specify a return value for the function. The return value is specified in the same form as for a type.

Named functions

No space is inserted between the function name and the parentheses. A space is inserted between the return type and the curly brackets.

Example
function functionName(param : string) : void {
  //...
}

Anonymous functions

No space is inserted between the function keyword and the parentheses. A space is inserted between the return type and the curly brackets.

Example
const fooBar = function(param : string) : void {
  //...
}

Arrow functions

A space is inserted between the parentheses and the arrow. A space is also inserted between the arrow and the curly brackets.

Example
const fooBar = (param : string) : void => {
  //...
}

class

...

namespace

...



↑ Back to top