Google TypeScript Style
gts (Google TypeScript Style) is an opinionated, zero-configuration linter, formatter, and automatic code fixer for TypeScript projects, maintained by the Google Node.js team. Currently at version 7.0.0, the package sees a regular release cadence with several updates annually, incorporating dependency bumps and style rule adjustments. Its core differentiators are its commitment to a single, enforced Google style without requiring manual configuration, leveraging ESLint for linting and Prettier for formatting under the hood. It aims to eliminate style bikeshedding and catch common programmer errors early in the development cycle, providing a consistent code style across projects. While primarily used via its CLI (`npx gts init`), it also exposes its ESLint configuration for direct integration.
Common errors
-
Error: Failed to load config "gts" from "/path/to/project/.eslintrc.js"
cause Attempting to use gts v7+ with the old ESLint `.eslintrc.js` configuration format.fixMigrate your ESLint configuration to the new flat config format in `eslint.config.js` as described in the v7.0.0 breaking changes. Example: `module.exports = [...require('gts')];` -
TS2728: Cannot find type definition file for 'node'.
cause TypeScript's `lib` configuration in `tsconfig.json` is incorrect for the Node.js version being targeted.fixEnsure your `tsconfig.json`'s `compilerOptions.lib` includes appropriate types for your Node.js version. gts v6.0.2 fixed an issue related to `lib` support for Node 18+. -
warning " > gts@6.0.1" has unmet peer dependency "typescript@>=5".
cause Your project's `typescript` version does not meet the `gts` peer dependency requirement.fixUpgrade TypeScript in your project to version 5 or greater. For example: `npm install typescript@latest --save-dev` or `yarn add typescript@latest --dev`. -
Error: A Promise-returning function provided to rule, 'no-floating-promises', is not marked async.
cause An async function is being used as an ESLint rule callback without being explicitly marked as `async`, leading to a false positive for `no-floating-promises`.fixMark the Promise-returning function as `async` in your ESLint configuration or custom rules to correctly handle asynchronous operations. If this is in your application code, ensure promises are handled.
Warnings
- breaking gts v7.0.0 migrated to ESLint's new flat config format (eslint.config.js). Projects using older `.eslintrc.js` files with gts may require updates to their ESLint configuration.
- breaking gts v6.0.0 updated the default Prettier configuration to include `trailingComma: "all"`. This will reformat code for projects that previously used a different `trailingComma` setting or no explicit setting.
- breaking gts v6.0.0 changed the `no-floating-promises` ESLint rule severity to `error`. This means any Promise returned from an async function that is not explicitly handled (e.g., awaited, `.then()`, `.catch()`) will now cause a build failure.
- breaking gts v6.0.0 set `composite: true` in `tsconfig-google.json`. This TypeScript configuration change affects how projects are built, particularly in monorepos or projects with multiple `tsconfig.json` files.
- gotcha gts requires Node.js version 18 or greater and TypeScript version 5 or greater as a peer dependency. Using older versions will lead to installation or runtime issues.
Install
-
npm install gts -
yarn add gts -
pnpm add gts
Imports
- ESLint configuration
import gts from 'gts';
module.exports = [...require('gts')]; - CLI usage
npx gts init
- Pre-commit hook
repos: - repo: https://github.com/google/gts rev: '' # Use the sha / tag you want to point at hooks: - id: gts
Quickstart
// Initialize a new TypeScript project with gts
// Run these commands in your terminal:
// mkdir my-gts-project
// cd my-gts-project
// npm init -y
// npx gts init
// This command initializes gts, adding necessary devDependencies,
// creating configuration files (tsconfig.json, .eslintrc.js, .prettierrc.json),
// and adding standard scripts to your package.json (e.g., 'lint', 'fix', 'compile').
// It also creates a default `src/index.ts` if no source directory exists.
// Example src/index.ts content after initialization:
// export function greet(name: string): string {
// return `Hello, ${name}!`;
// }
//
// async function main() {
// const message = greet('TypeScript');
// console.log(message);
// // Example of an ignored promise that would now be an error in gts v6+
// // Promise.resolve().then(() => console.log('async task'));
// }
//
// main().catch(console.error);
// To lint and fix issues after adding or modifying code:
// npm run lint
// npm run fix
// To compile your project:
// npm run compile