3

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):

  • static.js - which holds the declaration: var staticData = {...};, built automatically by grunt-json grunt task (nevermind what it does, it's just dynamically generated)
  • app.js - which is the typescript source dumped into js.

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.

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.