Skip to content

Instantly share code, notes, and snippets.

@statico
Last active February 7, 2019 09:03
Show Gist options
  • Save statico/77d2bec6e86890a2623dda0e2a0fa993 to your computer and use it in GitHub Desktop.
Save statico/77d2bec6e86890a2623dda0e2a0fa993 to your computer and use it in GitHub Desktop.
Top-level monorepo TypeScript/JavaScript style with tslint and Prettier

Goals

  • A single style for our projects and others
  • Minimize customization - Any decided-upon style is better than one we can bikeshed
  • Minimize friction - Instead of warnings, sources are formatted automatically on commit and on save inside your editor
  • Minimize files - These three or four files need only be at the top level of the monorepo... nothign in the subdirs

Why X?

  • Prettier, no semicolons - Because you don't need them and they add unnecessary visual clutter. Prettier will figure out when they're needed for you.
  • Prettier, single quotes - Reduces visual clutter - (Note: Still double quotes for element attributes in JSX/TSX)
  • Prettier, 80 chars (the default) - Because you can put more code up side-by-side. It'll be OK. I promise.
  • tslint "no-submodule-imports": false - Sometimes you need this in monorepos, especially for testing.
  • tslint "object-literal-sort-keys": false - Because sometimes alphanumeric isn't the best way to sort big literals or deconstructions.
{
"semi": false,
"singleQuote": true
}
{
"private": true,
"workspaces": [...],
"scripts": {
"lint": "tslint --project .",
"lint:fix": "tslint --fix --project ."
},
"devDependencies": {
"husky": "1.1.2",
"lint-staged": "7.3.0",
"prettier": "1.14.3",
"tslint": "5.11.0",
"tslint-config-prettier": "1.15.0",
"typescript": "^3.1"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.{js,json,css,md}": [
"prettier --write",
"git add"
],
"*.{ts,tsx}": [
"tslint --fix",
"prettier --write --parser=typescript",
"git add"
]
}
}
// This file is optional but might be necessary for TSX/React
{
"compilerOptions": {
"target": "esnext",
"module": "commonjs",
"jsx": "react",
"strict": true,
"resolveJsonModule": true
},
"exclude": ["node_modules"]
}
{
"extends": ["tslint:latest", "tslint-config-prettier"],
"rules": {
"interface-name": [true, "never-prefix"],
"object-literal-sort-keys": false,
"no-implicit-dependencies": [true, "dev"],
"no-submodule-imports": false
},
"linterOptions": {
"exclude": [
"node_modules/**",
"**/node_modules/**",
"**/*.d.ts",
"**/*.json"
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment