resolveJsonModule: Importing JSON Files
You'll learn how to enable TypeScript to import JSON files as if they were TypeScript modules, with automatic type safety.
What This Option Does
In many JavaScript projects, you store configuration data, translations, or other static information in JSON files. By default, TypeScript doesn't allow you to import these files directly like you would a .ts module. The resolveJsonModule compiler option changes that.
When you set resolveJsonModule: true in your tsconfig.json, TypeScript lets you import JSON files with a regular import statement. Even better, TypeScript automatically infers types from the JSON structure, giving you autocomplete and type checking without writing any extra code.
How It Works in Your Config
{
"compilerOptions": {
"resolveJsonModule": true,
"esModuleInterop": true
}
}
Note: resolveJsonModule requires esModuleInterop to be enabled as well.
Why This Matters
Imagine you have a config.json file with project settings. Without resolveJsonModule, you'd need to use require() or read the file manually, losing type safety. With this option enabled, TypeScript reads your JSON file and creates types automatically based on its structure.
This means if your JSON has { "port": 3000 }, TypeScript knows the imported object has a port property that's a number. Try accessing a non-existent property, and TypeScript catches it at compile-time.
The Safety Benefit
This bridges the gap between static data files and your type-safe TypeScript code, ensuring configuration mistakes surface before runtime.
Key Takeaway: Enabling resolveJsonModule lets you import JSON files as typed modules, bringing compile-time safety to your static data files.