When bringing in a JSON file into a TypeScript project with the resolveJsonModule
option activated, the TypeScript compiler can automatically determine the type of the imported JSON file. However, I find this type to be too specific and I would like to replace it with a more generic one. I attempted to achieve this by creating a custom type definitions file:
// Custom type definition file - modules.d.ts
declare module "*.json" {
const value: Record<string, string>;
export default value;
}
Unfortunately, the TypeScript compiler seems to be disregarding this definition.
The only workaround that has worked for me is using temporary variables, like so:
import DATA_JSON from "./data.json";
const DATA = DATA_JSON as Record<string, string>; // Utilizing my own type instead of the inferred one.
export { DATA };
However, this method becomes tedious. Is there an alternate approach to assigning custom types to JSON files imported in TypeScript?