Google TypeScript Style

repository·main·Indexed 26 days ago

https://github.com/google/gts

An opinionated configuration for formatters and linters designed to enforce Google's TypeScript style guide. It provides a zero-config experience using ESLint and Prettier under the hood, featuring a CLI for initializing projects, linting, fixing formatting issues, and cleaning build artifacts. It requires Node.js 10.x or higher.

Tokens
1.8K
Snippets
7
Records
14
Agent score
90%

What's inside gts

  1. Configure gts with pre-commit framework

    main

    To use gts as a hook in the pre-commit framework, add the following to your .pre-commit-config.yaml file. Ensure you specify a valid rev (tag or SHA).

    repos:
      - repo: https://github.com/google/gts
        rev: '' # Use the sha / tag you want to point at
        hooks:
          - id: gts
  2. Initialize gts in a project

    main

    To set up Google TypeScript Style in your project, run the init command. This will:

    • Add an opinionated tsconfig.json file.
    • Add necessary devDependencies to your package.json.
    • Add the following scripts to your package.json:
      • lint: Lints and checks for formatting problems.
      • fix: Automatically fixes formatting and linting problems.
      • clean: Removes output files.
      • compile: Compiles source code using the TypeScript compiler.
      • pretest, posttest, and prepare: Convenience integrations.
    • Add a default template project if no source folder is present.
    npx gts init
  3. Initialize a new project with gts

    main

    The init function automates the setup of a TypeScript project with Google's recommended configurations. It performs the following actions:

    1. package.json: Generates a default package.json if one doesn't exist, or updates existing ones with required devDependencies (gts, typescript, @types/node) and scripts.
    2. Configuration Files: Generates standard configuration files:
      • tsconfig.json: Extends gts/tsconfig-google.json.
      • eslint.config.js: Extends gts configuration.
      • eslint.ignores.js: Sets up default ignores (e.g., build/).
      • .prettierrc.js: Extends gts/.prettierrc.json.
      • .editorconfig: Sets standard indentation and encoding rules.
    3. Template: Installs a default src/ directory template if no .ts files are already present in the target directory.
    4. Dependency Installation: Automatically runs the package manager's install command (with --ignore-scripts) to ensure the environment is ready.

    Note: If options.dryRun is enabled, files will not be written to disk, but a preview of changes will be logged.

  4. Use gts as an ESLint configuration

    main

    You can use gts as a shared configuration for ESLint. Create an eslint.config.js file in your project directory and extend the shared config using require('gts').

    module.exports = [
      ...require('gts'),
    ];
  5. Configure ESLint using gts

    main

    The gts package exports a pre-configured ESLint configuration using defineConfig. It provides a comprehensive set of rules for JavaScript and TypeScript, including integration with Prettier, eslint-plugin-n, and typescript-eslint.

    When you require gts in your eslint.config.js, it applies:

    • ESLint recommended rules.
    • Prettier configuration to avoid conflicts.
    • Strict rules for variable scoping (no-var, prefer-const), equality (eqeqeq), and formatting.
    • Restrictions on using .only in describe or it blocks.
    • TypeScript-specific rules for .ts and .tsx files, including parser settings for tsconfig.json and recommended @typescript-eslint rules.
  6. Configure ESLint with gts

    main

    To use gts with ESLint, use the defineConfig utility from eslint/config to merge the gts configuration with your project's specific ignores. The gts configuration is imported from ./src/index.js and is spread into the configuration array.

    'use strict';
    const config = require('./src/index.js');
    const ignores = require('./eslint.ignores.js');
    const defineConfig = require('eslint/config').defineConfig;
    
    module.exports = defineConfig([{ignores}, ...config]);
  7. Lint individual files with gts

    main

    The gts lint command can be scoped to specific files or patterns rather than the entire project directory.

    gts lint index.ts
    gts lint one.ts two.ts three.ts
    gts lint *.ts
  8. Clean build artifacts using the clean function

    main

    The clean function removes files generated by the build process. It identifies the directory to delete by reading the compilerOptions.outDir setting from the tsconfig.json file located in the project's target root directory.

    Requirements:

    • The tsconfig.json must have compilerOptions.outDir explicitly defined.
    • The outDir value cannot be . (the current directory), as this would attempt to delete all source files.

    If outDir is not defined or is set to ., the function will log an error and return false. Otherwise, it recursively removes the directory and returns true.

  9. Install the default gts template

    main

    The installDefaultTemplate function populates a src/ directory with a default project structure.

    Constraint: The template is only installed if the target src/ directory does not already contain any .ts files. If .ts files are detected, the installation is skipped to prevent overwriting existing work.

  10. Configure gts CLI options

    main

    When running gts commands, you can use the following options to modify behavior:

    OptionAliasDescription
    --helpPrints the help message
    --yes-yAssume a yes answer for every prompt
    --no-nAssume a no answer for every prompt
    --dry-runDon't make any actual changes
    --yarnUse yarn instead of npm
    --help
    -y, --yes
    -n, --no
    --dry-run
    --yarn
  11. Configure package.json scripts for gts

    main

    When using addScripts, gts ensures your package.json contains the following standard scripts:

    ScriptCommand
    lintgts lint
    cleangts clean
    compiletsc
    fixgts fix
    prepare<pkg-manager> run compile
    pretest<pkg-manager> run compile
    posttest<pkg-manager> run lint

    The <pkg-manager> is determined by your configuration (e.g., yarn if enabled).