Best Practices and Evolution of Importing JSON Files in TypeScript

Nov 24, 2025 · Programming · 10 views · 7.8

Keywords: TypeScript | JSON Import | Angular Map Application | Module Resolution | Compiler Configuration

Abstract: This article provides an in-depth exploration of the technical evolution for importing JSON files in TypeScript projects, from traditional type declaration methods to native support in TypeScript 2.9+. Through detailed code examples and configuration instructions, it demonstrates how to dynamically import JSON marker data in Angular map applications, avoiding the use of hardcoded arrays. The article also analyzes the functional principles of different configuration options and offers complete implementation solutions for practical application scenarios.

Technical Evolution Background of JSON File Import

In modern web application development, particularly in map application scenarios based on the Angular framework, developers often need to import external data sources in JSON format into TypeScript files. Traditional hardcoded array approaches are not only difficult to maintain but also lack flexibility. With the continuous evolution of the TypeScript language, support solutions for JSON file imports have undergone a complete development path from third-party solutions to native support.

Traditional Solution: Type Declaration File Method

In early versions of TypeScript, it was necessary to manually create type declaration files to support JSON module imports. The specific implementation involves creating a typings.d.ts file in the project root directory with the following content:

declare module "*.json" {
    const value: any;
    export default value;
}

The principle of this method is to inform the TypeScript compiler through module declaration that all files ending with .json should be treated as modules with default exports. After completing the declaration, developers can use standard ES6 import syntax:

import * as markerData from './locations.json';

In the specific implementation of map applications, this import approach allows developers to completely externalize marker data, achieving separation of data and code, significantly improving project maintainability.

Modern Solution: Native Support in TypeScript 2.9+

TypeScript version 2.9 introduced the resolveJsonModule compiler option, providing a more elegant native solution. First, configuration in the tsconfig.json file is required:

{
  "compilerOptions": {
    "resolveJsonModule": true,
    "esModuleInterop": true
  }
}

After configuration, simplified import syntax can be used directly:

import markerData from './locations.json';

The advantage of this method is that the TypeScript compiler can automatically infer the structural type of JSON files, providing complete IntelliSense support. For marker arrays in map applications, the compiler can recognize array methods, greatly enhancing the development experience.

Special Configuration in Angular Environment

In Angular 7+ projects, to ensure compatibility of JSON imports, it is recommended to configure complete compiler options in tsconfig.json:

{
  "compilerOptions": {
    "resolveJsonModule": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true
  }
}

The esModuleInterop option ensures interoperability with CommonJS modules, while allowSyntheticDefaultImports allows default imports from modules without default exports. The combination of these three options provides the most stable support environment for JSON imports.

Practical Application Case: Map Marker Data Import

Assuming we have a JSON file locations.json containing map marker information:

[
  {
    "lat": 40.7128,
    "lng": -74.0060,
    "title": "New York City"
  },
  {
    "lat": 34.0522,
    "lng": -118.2437,
    "title": "Los Angeles"
  }
]

In the TypeScript file of an Angular component, we can import and use it as follows:

import { Component } from '@angular/core';
import markerData from './locations.json';

@Component({
  selector: 'app-map',
  template: `
    <google-map>
      <map-marker 
        *ngFor="let marker of markers"
        [position]="{lat: marker.lat, lng: marker.lng}"
        [title]="marker.title">
      </map-marker>
    </google-map>
  `
})
export class MapComponent {
  markers: any[] = markerData;
}

This implementation approach completely avoids hardcoding, making updates and maintenance of marker data extremely convenient. Developers only need to modify the JSON file without touching the TypeScript source code.

In-depth Analysis of Technical Principles

The working principle of the resolveJsonModule option is to enable the TypeScript compiler to read JSON file content during the compilation phase and convert it into equivalent TypeScript type declarations. This process is type-safe, with the compiler validating the effectiveness of the JSON structure and providing type checking during the development phase.

For other TypeScript-derived languages like AssemblyScript, support for JSON imports may vary. As shown in the reference article, AssemblyScript in specific versions might not yet support native JSON import functionality, reflecting compatibility differences between different implementations.

Best Practice Recommendations

In actual project development, the following best practices are recommended: always use the native JSON import solution of TypeScript 2.9+; unify tsconfig.json configuration in team projects; create clear interface definitions for JSON data structures to enhance type safety; validate the syntactic correctness of JSON files in the build process.

By following these practices, developers can build more robust and maintainable TypeScript applications, especially in data-intensive application scenarios like map services.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.