3.0.9 • Published 2 days ago

@moia-oss/eslint-prettier-typescript-config v3.0.9

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
2 days ago

eslint-prettier-typescript-config

Shared MOIA TypeScript, eslint and prettier configuration

Usage

  1. Install

    npm install -D typescript eslint prettier
    npm install -D @moia-oss/eslint-prettier-typescript-config
    
    # in some cases, you may need to install these packages directly:
    npm install -D @typescript-eslint/parser @typescript-eslint/eslint-plugin
  2. Link configurations

    (customise paths as needed)

    • tsconfig.json

      {
        "extends": "@moia-oss/eslint-prettier-typescript-config",
        "compilerOptions": {
          "outDir": "./build",
          "rootDir": "./src",
        },
        "include": ["./src"],
      }
    • .eslintrc (remove react and strict if not required. more info further down in this README).

      {
        "extends": [
          "./node_modules/@moia-oss/eslint-prettier-typescript-config/config/eslint",
          "./node_modules/@moia-oss/eslint-prettier-typescript-config/config/eslint-react",
          "./node_modules/@moia-oss/eslint-prettier-typescript-config/config/eslint-strict",
        ],
        // Only add if you have a CDK directory, customise path as needed
        "overrides": [
          {
            "files": ["cdk/**/*"],
            "extends": [
              "./node_modules/@moia-oss/eslint-prettier-typescript-config/config/eslint-cdk",
            ],
          },
        ],
      }
    • .prettierrc

      "@moia-oss/eslint-prettier-typescript-config/config/prettier"
  3. Add scripts to package.json

    (customise paths as needed)

    {
      "scripts": {
        "build": "tsc",
        "lint": "eslint ./src/",
        "format": "prettier . --write",
        "format:check": "prettier . --check",
      },
    }
  4. Done! Don't forget to run build, lint and format:check in your CI workflow.

Strict eslint mode

The strict config enables a few more things that not every team may want:

  • Arrow (expression) function style preferred:

    // Not allowed
    
    function foo() {
      return 1;
    }
    
    // Allowed
    
    const foo = () => {
      return 1;
    };
  • Type assertions not allowed:

    Due to the design goal of type erasure (no runtime overhead), type assertions are not ever checked.

    When you assert a type, it may look suspiciously like type casting in a language such as Kotlin, but it isn't the same thing. TypeScript just "trusts" you and doesn't check the type.

    At runtime, you wouldn't get a cast error if the type is not the same, but only see a problem if you try to access or use a value that doesn't match the expected type.

    Use parsing libraries such as zod.

    // Error:
    
    type Foo = {
      x: 1;
    };
    // no error thrown, silently continues
    const foo = JSON.parse('{"y":1}') as Foo;
    
    // evauluates silently to NaN:
    foo.x + 1;
    
    // this also silently continues:
    const bar = JSON.parse('{"xyz":1}') as { x: { y: number } };
    
    // you won't see a problem in runtime until somewhere
    // else in the code, making it hard to trace:
    
    // throws `Uncaught TypeError: Cannot read property 'y' of undefined`
    console.log(bar.x.y);
    // OK:
    import * as z from 'zod';
    
    const FooSchema = z.object({
      x: z.number(),
    });
    
    type Foo = z.infer<typeof FooSchema>;
    
    // parses successfully
    const foo = FooSchema.parse(JSON.parse('{"x":1}'));
    
    // throws an error detailing why the JSON doesn't match
    const bar = FooSchema.parse(JSON.parse('{"y":1}'));

Optional Additions

  • VSCode lint/format settings in .vscode/settings.json

    {
      "editor.formatOnSave": true,
      "editor.defaultFormatter": "esbenp.prettier-vscode",
      "editor.codeActionsOnSave": {
        "source.fixAll.eslint": true,
      },
    }
  • VSCode extension recommendations in .vscode/extensions.json

    { "recommendations": ["esbenp.prettier-vscode", "dbaeumer.vscode-eslint"] }
  • .editorconfig

    [*]
    end_of_line = lf
    insert_final_newline = true
    charset = utf-8
    indent_style = space
    indent_size = 2

Config Development

This relates to the development of this package. Ignore this section as a consumer.

This package uses its own exported config to build, lint and format itself. This also makes sure that the configs are valid, as the steps are run during the GitHub Actions build step.

Because of this, you must run npm run build before linting or formatting during development

Husky hooks

Husky is used to introduce git hooks. Link to the package https://typicode.github.io/husky/ To install them, execute npm run husky:prepare

Release

We will automatically release on push to main. Make sure to update the package version according to your change following semantic versioning

3.0.9

2 days ago

3.0.8

16 days ago

3.0.7

23 days ago

3.0.6

1 month ago

3.0.5

1 month ago

3.0.4

2 months ago

3.0.3

2 months ago

3.0.2

2 months ago

3.0.1

3 months ago

3.0.0

3 months ago

3.0.0-next.0

3 months ago

2.0.26

3 months ago

2.0.25

3 months ago

2.0.24

4 months ago

2.0.23

4 months ago

2.0.22

4 months ago

2.0.21

5 months ago

2.0.20

5 months ago

2.0.7

8 months ago

2.0.9

8 months ago

2.0.8

8 months ago

2.0.0

10 months ago

1.0.1

10 months ago

1.0.0

10 months ago

0.11.67

10 months ago

2.0.15

6 months ago

2.0.16

6 months ago

2.0.13

7 months ago

2.0.14

7 months ago

2.0.11

7 months ago

2.0.12

7 months ago

2.0.10

7 months ago

2.0.19

5 months ago

2.0.17

6 months ago

2.0.18

6 months ago

0.11.66

11 months ago

0.11.59

1 year ago

0.11.58

1 year ago

0.11.62

11 months ago

0.11.63

11 months ago

0.11.64

11 months ago

0.11.65

11 months ago

0.11.60

12 months ago

0.11.61

12 months ago

0.11.49

1 year ago

0.11.51

1 year ago

0.11.52

1 year ago

0.11.53

1 year ago

0.11.54

1 year ago

0.11.50

1 year ago

0.11.55

1 year ago

0.11.56

1 year ago

0.11.57

1 year ago

0.11.48

1 year ago

0.11.45

1 year ago

0.11.46

1 year ago

0.11.47

1 year ago

0.11.37

1 year ago

0.11.38

1 year ago

0.11.39

1 year ago

0.11.35

1 year ago

0.11.36

1 year ago

0.11.40

1 year ago

0.11.41

1 year ago

0.11.42

1 year ago

0.11.43

1 year ago

0.11.44

1 year ago

0.11.30

2 years ago

0.11.31

2 years ago

0.11.32

2 years ago

0.11.33

2 years ago

0.11.34

2 years ago

0.11.26

2 years ago

0.11.27

2 years ago

0.11.28

2 years ago

0.11.29

2 years ago

0.11.25

2 years ago

0.11.20

2 years ago

0.11.21

2 years ago

0.11.22

2 years ago

0.11.23

2 years ago

0.11.24

2 years ago

0.11.19

2 years ago

0.11.18

2 years ago

0.11.15

2 years ago

0.11.16

2 years ago

0.11.17

2 years ago

0.11.12

2 years ago

0.11.13

2 years ago

0.11.14

2 years ago

0.11.10

2 years ago

0.11.11

2 years ago

0.11.8

2 years ago

0.11.9

2 years ago

0.11.2

2 years ago

0.11.3

2 years ago

0.11.4

2 years ago

0.11.5

2 years ago

0.11.6

2 years ago

0.11.7

2 years ago

0.11.0

2 years ago

0.10.1

2 years ago

0.11.1

2 years ago

0.10.2

2 years ago

0.9.21

3 years ago

0.9.19

3 years ago

0.9.20

3 years ago

0.9.17

3 years ago

0.9.18

3 years ago

0.9.16

3 years ago

0.9.15

3 years ago

0.9.14

3 years ago

0.9.13

3 years ago

0.9.12

3 years ago

0.9.11

3 years ago

0.9.10

3 years ago

0.9.9

3 years ago

0.9.8

3 years ago

0.9.7

3 years ago

0.9.6

3 years ago

0.9.5

3 years ago

0.9.4

3 years ago

0.9.3

3 years ago

0.9.2

3 years ago

0.9.0

3 years ago

0.9.1

3 years ago

0.8.4

3 years ago

0.8.3

3 years ago

0.8.2

3 years ago

0.8.1

3 years ago

0.8.0

3 years ago