From 16d8a78d8a614a07886a99f696a6efaa76435f01 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Sat, 5 Aug 2023 15:04:56 +0100 Subject: [PATCH] Doc: Add rule "Do not set the type when it can be inferred" --- readme/coding_style.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/readme/coding_style.md b/readme/coding_style.md index 1e0ca94f0..c4ff30bad 100644 --- a/readme/coding_style.md +++ b/readme/coding_style.md @@ -52,6 +52,28 @@ const config: Config = { } ``` +### Don't set the type when it can be inferred + +TypeScript can automatically detect the type so setting it explicitely in many cases is not needed, and makes the code unecessary verbose. We already have enabled the eslint rule `no-inferrable-types`, however it only applies to simple types such as string, number, etc. but not to function calls. + +**BAD:** +```ts +const getSomething():string => { + return 'something'; +} + +const timestamp:number = Date.now(); +``` + +**Good:** +```ts +const getSomething() => { + return 'something'; +} + +const timestamp = Date.now(); +``` + ## Filenames * `camelCase.ts`: Files that export multiple things.