TypeScript .gitignore Guide
October 26, 2023
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
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.
Common TypeScript Exclusions
.js extension and source maps have the
.js.map extension. If you're compiling TypeScript to a dedicated output directory (e.g.,
build), you might want to exclude the entire directory. If not, you might want to exclude the individual files.
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.
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.
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.
TypeScript might generate cache files when using project references or when incremental compilation is enabled.
Other Common Exclusions
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.
How to fix the "not all code paths return a value" issue in TypeScript
Working with WebSockets in Node.js using TypeScript
Type Annotations Can Only Be Used in TypeScript Files
Guide to TypeScript Recursive Type
How to Configure Knex.js with TypeScript