These are some rules for my TypeScript projects.
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
.
The filename would look like this:
encode_base64.ts
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
, ...
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.
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 Filenameread_text_file.ts
.
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.
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.
The maximum character limit for a line is 120 characters and a maximum of 4 indentations.
All TypeScript files are documented in the JSDoc format.
All variables are written in camelcase. You can read more about that in the let
/const
/var
section.
const greeting = 'Hello World'; let age : number;
Spaces are inserted between a variable and the colon and between the colon and a type.
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.
const object = {key: 'Text'};
let
/const
names are written in camelcase. The same is applied to members of objects. Avoid the use of var
, use let
/const
instead.
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.
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.
const API_ENDPOINT = 'https://api.example.com';
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.
interface FooBar { text : string, value : number }
...
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.
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
.
export const textTypes = { TEXT_A: 0, TEXT_B: 1 } as const export typeof TextType[keyof typeof textTypes]
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.
No space is inserted between the function name and the parentheses. A space is inserted between the return type and the curly brackets.
function functionName(param : string) : void { //... }
No space is inserted between the function
keyword and the parentheses. A space is inserted between the return type and the curly brackets.
const fooBar = function(param : string) : void { //... }
A space is inserted between the parentheses and the arrow. A space is also inserted between the arrow and the curly brackets.
const fooBar = (param : string) : void => { //... }
...
...