This TypeScript compilation error occurs when you use the --outDir flag without providing TypeScript source files (.ts or .tsx extensions). The TypeScript compiler needs source files to determine what to compile and where to output the JavaScript files.
When you run the TypeScript compiler (tsc) with the --outDir flag, you're telling it to output compiled JavaScript files to a specific directory. However, the compiler needs to know which TypeScript source files to compile. This error typically happens in two scenarios: 1. You're running tsc with --outDir but not specifying any .ts or .tsx files on the command line 2. You're using a tsconfig.json file that doesn't properly specify the files or include patterns The TypeScript compiler requires explicit source files when using --outDir because it needs to know what to compile and where to place the output relative to the source file structure. Without source files, it cannot determine the output directory structure.
Verify you're specifying TypeScript source files when using --outDir:
# Correct: Specify .ts files with --outDir
tsc --outDir dist src/*.ts
# Correct: Use tsconfig.json instead
tsc --outDir dist --project tsconfig.json
# Incorrect: Missing .ts files
tsc --outDir dist # This will cause the errorAlways include .ts or .tsx files when using --outDir on the command line.
Instead of specifying files on the command line, create or use a tsconfig.json file:
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
}Then compile with:
tsc
# or
tsc --project tsconfig.jsonThe tsconfig.json file tells TypeScript which files to compile and where to output them.
If you're using glob patterns, ensure they match .ts or .tsx files:
# These patterns work with --outDir
tsc --outDir dist src/**/*.ts
tsc --outDir dist *.tsx
tsc --outDir dist app.ts utils.ts
# These will cause errors (no .ts files)
tsc --outDir dist *.js
tsc --outDir dist src/**/* # Might match non-TypeScript filesUse explicit .ts/.tsx extensions or ensure your glob patterns only match TypeScript files.
If using tsconfig.json, verify your include patterns match TypeScript files:
{
"compilerOptions": {
"outDir": "dist"
},
"include": [
"src/**/*.ts", // Explicit .ts files
"src/**/*.tsx" // Explicit .tsx files
],
"exclude": [
"node_modules",
"**/*.spec.ts", // Test files if needed
"**/*.test.ts"
]
}Common issues:
- "include": ["src"] - includes directories but not file patterns
- "include": ["src/**/*"] - might include non-TypeScript files
- Missing include/exclude sections entirely
When you have multiple tsconfig files or need to specify a configuration:
# Use a specific tsconfig file
tsc --project tsconfig.prod.json
# Compile from a directory containing tsconfig.json
tsc --project ./src
# Build mode for project references
tsc --build --verboseThe --project flag tells TypeScript to use the specified tsconfig.json file or directory containing one.
## Understanding TypeScript Compilation Modes
TypeScript has two main compilation modes:
1. File-based compilation: When you specify files on the command line, TypeScript ignores tsconfig.json and compiles only those files. This mode requires explicit file specifications when using --outDir.
2. Project-based compilation: When you use tsconfig.json (either implicitly or with --project), TypeScript uses the configuration to determine which files to compile.
## Common Pitfalls
- Mixed file types: Specifying both .ts and .js files with --outDir will fail because .js files aren't compiled.
- Empty directories: If your include patterns don't match any files, you'll get a different error about no inputs.
- Symlinks and aliases: Complex project structures with symlinks might cause unexpected behavior.
## Alternative Approaches
If you need to output compiled JavaScript to a specific directory without specifying source files, consider:
- Using a build tool like Webpack, Rollup, or esbuild with TypeScript integration
- Creating a shell script that finds all .ts files and passes them to tsc
- Using the TypeScript compiler API programmatically
Type parameter 'X' is not used in the function signature
How to fix "Type parameter not used in function signature" in TypeScript
Type parameter 'X' is defined but never used
How to fix "Type parameter is defined but never used" in TypeScript
Function expression requires a return type
Function expression requires a return type
Value of type 'string | undefined' is not iterable
How to fix "Value is not iterable" in TypeScript
Type 'undefined' is not assignable to type 'string'
How to fix "Type undefined is not assignable to type string" in TypeScript