#Are declaration files supported for large JSON files?

4 messages · Page 1 of 1 (latest)

latent swan
#

I'm currently importing a large search index (43MB) via

// sample.ts
import index from './index.json';
// ...

TS server skips the file for being too large, which types index as {}. This results in errors such as Property 'xyz' does not exist on type '{}'. ts(2339).

That behavior is reasonable to me, but providing a declaration file (index.d.ts) for this import doesn't appear to workaround this. index remains {}.

// index.d.ts

declare module "*.json" {
    const value: {
        xyz: {
            [key: string]: number;
        }
    }

    export default value;
}

Q: Are declaration files used/supported for JSON imports? (I can't get them to have any effect small or large! I've verified TS server is finding/loading them.)

Q: Is there an alternate approach I should take here with regards to loading large JSON files?

Some meta information:

  • Started with create-react-app
  • App generally compiles and works (without strict mode)
  • tsconfig.json snippet:
 "resolveJsonModule": true,
 "allowSyntheticDefaultImports": true,
 "esModuleInterop": true,
 "module": "esnext",
 "moduleResolution": "node",
fluid river
#

Try turning off resolveJsonModule if you want to supply your own declaration for JSON files.

latent swan
#

Wow. 🤦‍♂️ That works and makes sense, totally missed that.

#

Thanks @fluid river