Reputation: 26477
I've got an application that is migrated into typescript. In my index.html file I've got 2 files (actually, a lot more, but those are significant):
var staticData = {...};
, built automatically by grunt-json
grunt task (nevermind what it does, it's just dynamically generated)There is .ts a file that is used to build the app.js
, which has following content:
mp.core.CoreModule
.constant('CONFIG', staticData.config)
.constant('lodash', _)
It's just declaring angular constants. When I try to compile this typescript file, I get following error:
Running "typescript:app" (typescript) task
>> app/modules/core/coreModuleConfig.ts(2,21): error TS2304: Cannot find name 's
taticData'.
>> app/modules/core/coreModuleConfig.ts(3,21): error TS2304: Cannot find name '_
'.
Warning: Task "typescript:app" failed. Use --force to continue.
I can understand the problem - typescript doesn't know what these variables are. The problem is that staticData is a js-file variable and I've got no idea how I can make those file cope with each other... Lodash' case is the same - it's loaded into the HTML as an external script.
Upvotes: 3
Views: 2384
Reputation: 106820
A quick solution is to use ambient definitions with an any
type:
declare var staticData: any;
declare var _: any;
That will allow you to use those variables in any way you want in your TypeScript. From this, you could start building on type information for staticData
if you wanted.
Note that for _
, you can just include the lodash.d.ts
definition file in your project. That already has all the type information for lodash defined for you. You can find that here.
Upvotes: 4