It's time to ditch Medium for good! 🌈⚡️

Introducing Devblog by Hashnode. Blog on your domain for FREE. Highly customizable and optimized for developers.

Learn more

How to use a JSON file in TypeScript?

RE:

Thanks to TypeScript 2.x Wildcard Module Declarations you can do the following:

Somewhere in a *.d.ts file add this (or check if its already there):

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

Then you can do things like this in your TypeScript code:

import * as data1 from './data/some.json';
import data2 from "json!http://foo.com/data_returns_json/";

Note: I think this is already part of the ES6/ES2015 standard if you're writing plain JS targeting ES6/ES2015.

Show all replies

Doesn't work for SystemJs :(

Reply to this…

(3 answers) Take me to the question