Wednesday, June 6, 2018

I can't suck in a JSON object in an independent .json file with a typical import in TypeScript.

That's correct! The way around that is to write a function like this at the top of the TypeScript file where you will need to get at the JSON object...

declare function require(moduleName: string): any;

 
 

...and then have at the JSON object's guts like so...

let cicada = require('../../cricket.json').katydid;

 
 

...this seemed like another example of TypeScript not being a superset of JavaScript to me. I didn't see why the second line of code wouldn't work standalone and apparently it will work in mere JavaScript. It was explained to me that the reason that TypeScript can't understand require to compile it is that is it implemented at most modern browsers through their own prototype provided bolt-ons and it is not technically standard JavaScript. Boo hoo! Above we are fishing the katydid property out of the JSON object and assigning it to the cicada variable.

No comments:

Post a Comment