This TypeScript error occurs when you use the --listFilesOnly compiler flag without also enabling --listFiles. The --listFilesOnly option is dependent on --listFiles and requires it to be set first. The fix involves either adding --listFiles to your command or tsconfig.json, or using the correct standalone flag --listEmittedFiles instead.
The "'--listFilesOnly' requires --listFiles" error appears when you try to use TypeScript's --listFilesOnly compiler option without first enabling the --listFiles option. These are related but distinct compiler flags with specific dependencies. --listFiles is a TypeScript compiler option that prints all files read during compilation, including library files and source files. This is useful for debugging which files TypeScript is including in your build. --listFilesOnly is a variant that depends on --listFiles being enabled. It's designed to work in conjunction with --listFiles, not as a standalone option. When you use --listFilesOnly without --listFiles, TypeScript cannot determine which files to list because the base listing functionality isn't active. The error indicates a misunderstanding of how these compiler options interact. TypeScript requires --listFiles to be enabled first to establish the file listing context, then --listFilesOnly can filter or modify that output.
When using --listFilesOnly from the command line, you must also include --listFiles:
# WRONG - missing --listFiles
tsc --listFilesOnly
# CORRECT - include both flags
tsc --listFiles --listFilesOnly
# You can also use the short form
tsc --listFiles --listFilesOnly src/**/*.tsThe order doesn't matter, but both flags must be present. After adding --listFiles, the command should execute successfully and list the files.
If you're configuring these options in your tsconfig.json file, set both properties:
{
"compilerOptions": {
"listFiles": true,
"listFilesOnly": true,
// other options...
}
}Then run TypeScript normally:
npx tsc
# or
npm run buildWith both flags enabled, TypeScript will list the files it processes during compilation.
If you want to see only the output files (JavaScript, declaration files, etc.) that TypeScript generates, use --listEmittedFiles instead. This is a standalone flag that doesn't require --listFiles:
# Shows only generated output files
tsc --listEmittedFiles
# In tsconfig.json
{
"compilerOptions": {
"listEmittedFiles": true,
// No need for listFiles or listFilesOnly
}
}Key differences:
- --listFiles: Shows all input files (source + libraries)
- --listFilesOnly: Requires --listFiles, shows filtered list
- --listEmittedFiles: Standalone, shows only output files (.js, .d.ts, etc.)
Verify your TypeScript version and consult the official documentation:
# Check TypeScript version
npx tsc --version
# View help for listFiles options
npx tsc --help | grep -i listConsult the TypeScript compiler options documentation:
- [TypeScript Compiler Options](https://www.typescriptlang.org/tsconfig)
- [Command Line Interface](https://www.typescriptlang.org/docs/handbook/compiler-options.html)
Note: In TypeScript 4.2+, consider using --explainFiles instead of --listFiles for more detailed information about why files were included.
If you encounter this error in automated scripts or CI/CD pipelines, update the configuration:
# Before (failing)
npm run build -- --listFilesOnly
# After (working)
npm run build -- --listFiles --listFilesOnlyFor package.json scripts:
{
"scripts": {
"list-files": "tsc --listFiles --listFilesOnly",
"list-emitted": "tsc --listEmittedFiles"
}
}Run the updated scripts:
npm run list-files
npm run list-emittedFor TypeScript 4.2 and later, use --explainFiles instead for better debugging:
# Modern alternative to --listFiles
tsc --explainFiles
# Output shows why each file was included:
# - Because it matches include pattern in tsconfig.json
# - Because it's imported by another file
# - Because it's a library file
# - etc.In tsconfig.json:
{
"compilerOptions": {
"explainFiles": true
}
}--explainFiles provides more context than --listFiles and doesn't have the dependency issues of --listFilesOnly. It's the recommended approach for debugging file inclusion in modern TypeScript projects.
### Historical Context and Flag Evolution
The --listFiles and --listFilesOnly flags have an interesting history in TypeScript:
- TypeScript 1.5: --listFiles introduced as a debugging tool
- TypeScript 2.0: --listEmittedFiles introduced as a separate, standalone flag
- Undocumented feature: --listFilesOnly existed as a dependent flag but wasn't well-documented
- TypeScript 4.2: --explainFiles introduced as a modern replacement with better output
The confusion around --listFilesOnly stems from it being a "hidden" or poorly documented feature that depended on --listFiles. Many developers discovered it through trial and error or community knowledge sharing rather than official documentation.
### Alternative Debugging Approaches
Beyond compiler flags, consider these debugging techniques:
1. --traceResolution: Trace module resolution to see how TypeScript finds files
npx tsc --traceResolution2. --diagnostics: Show diagnostic information including file counts
npx tsc --diagnostics3. TypeScript Server Logs: In VS Code, enable TypeScript server logging:
- Open Command Palette (Cmd/Ctrl+Shift+P)
- Type "TypeScript: Open TS Server Log"
- Or set "typescript.tsserver.log": "verbose" in settings
### Understanding the Dependency Chain
The dependency between --listFiles and --listFilesOnly exists because:
1. --listFiles enables the file tracking system
2. --listFilesOnly queries that tracking system
3. Without --listFiles, there's no tracking system to query
This is similar to other dependent flags in TypeScript and other compilers where:
- A base flag enables a feature
- A modifier flag adjusts that feature
- The modifier cannot work without the base
### Migration to --explainFiles
If you're using --listFiles/--listFilesOnly for debugging, consider migrating to --explainFiles:
Benefits:
- Standalone flag (no dependencies)
- More informative output
- Shows reasoning for file inclusion
- Better integration with modern TypeScript features
- Actively maintained and documented
Migration example:
# Old approach
tsc --listFiles --listFilesOnly
# New approach
tsc --explainFilesThe output format differs but provides more valuable information for debugging file inclusion issues.
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