I am running into an issue with JSON TypeScript imports being wrapped in a module in an inconsistent manner. Here is a reduced test case:
Things.ts:
import * as a from "./a.json";
import * as b from "./b.json";
export class Things {
public static one: any;
public static two: any;
public static three: any;
public static loadThings() {
Things.one = a.one;
Things.two = a.two;
Things.three = b;
}
}
a.json: { "one": 1, "two": 2 }
b.json: { "three": 3 }
From the following test code:
Things.loadThings();
console.log(Things.one, Things.two, Things.three);
I would expect: 1 2 {three: 3}
, but I get 1 2 Module {default: {three: 3}, __esModule: true, Symbol(Symbol.toStringTag): 'Module'}
My temporary fix is to include a helper function that unwraps the import:
const unwrapJson = <T>(imported: T): T => (imported as any).default as typeof imported;
Things.three = unwrapJson(b);
I would prefer to avoid this. I believe this is something related to esModuleInterop being disabled. But then why does TypeScript know to correctly resolve the import in the file the import takes place in, but does not when referred to outside? Is there a way I can make this behavior consistent without needing the helper function?
Unlike this question Understanding esModuleInterop in tsconfig file I am wondering about the inconsistency of the issue rather than why there is the wrapping.