TypeScript .gitignore Guide

When working with TypeScript, certain files and directories are generated during the compilation and development process that you may not want to commit to your Git repository. This guide will outline the common items you'd typically want to exclude using a .gitignore file.

Getting Started

First, make sure you're in the root directory of your TypeScript project. Then create a file named .gitignore if it doesn't already exist.

touch .gitignore

Common TypeScript Exclusions

Compilation Outputs

TypeScript compiles down to JavaScript, and by default, the compiled JavaScript files have the .js extension and source maps have the .js.map extension. If you're compiling TypeScript to a dedicated output directory (e.g., dist or build), you might want to exclude the entire directory. If not, you might want to exclude the individual files.

# If using a dedicated output directory: dist/ build/ # If not using a dedicated output directory: *.js *.js.map

Node Modules

If you're using npm or Yarn to manage your project's dependencies, you'll have a node_modules/ directory. This should always be excluded as it can contain thousands of files, and you don’t want those in your repo.

node_modules/

IDE and Editor Configurations

Many IDEs and editors create configuration and cache files that are specific to a user's environment. Examples include .vscode/ for Visual Studio Code and .idea/ for JetBrains IDEs.

.vscode/ .idea/

Environment Configuration

Files that contain secrets, API keys, or database connection info (commonly .env files or other configuration files) should never be committed to a public repository.

.env .env.local .env.*.local

TypeScript Cache

TypeScript might generate cache files when using project references or when incremental compilation is enabled.

*.tsbuildinfo

Other Common Exclusions

Other files and directories that are commonly excluded in many JavaScript and TypeScript projects include:

# Log files *.log # Runtime data pids *.pid *.seed *.pid.lock # Directory for instrumented libs generated by jscoverage/JSCover lib-cov # Coverage directory used by tools like istanbul coverage # nyc test coverage .nyc_output # Grunt intermediate storage (<https://gruntjs.com/creating-plugins#storing-task-files>) .grunt # Bower dependency directory (<https://bower.io/>) bower_components # Dependency directory # Commenting this out is preferred by some developers, npm can # handle it properly when it's symlinked (npm v3+) # node_modules/ # TSD Debug info tsd-debug.log

Wrapping Up

Once you've configured your .gitignore based on your needs, simply save and close the file. Git will now respect these exclusions for all future commits. Remember, if you've previously committed unwanted files, you'll need to remove them from your repository history or use the git rm command before the .gitignore changes will apply to them.

Invite only

We're building the next generation of data visualization.