3

I am trying to use the Salesforce JavaScript SDK in a Typescript project.

It has a file called canvas-all.js. That has a variable called Sfdc.

We have some javascript that uses this successfully. All it needs to do to use it is add this to the index.html

<script type="text/javascript" src="./plugins/canvas-sdk/js/canvas-all.js"></script>

But Typescript does not know about it from that. (Says Cannot find name Sfdc)

I tried to just import it like this:

import * as canvas from '../plugins/canvas-sdk/js/canvas-all';

But that gives the error:

Cannot find module '../plugins/canvas-sdk/js/canvas-all'.

I am thinking I need a .d.ts for this, but I can't find one, and I am not succeeding in trying to make one.

I have looked at this aricle: https://www.stevefenton.co.uk/2013/01/complex-typescript-definitions-made-easy/ but it does not seem to help.

Step 2 in that article is for those that just want Typescript to be unblocked (my scenario). But all it says is declare var amazingToolkit: any (It does not have any context on where to put it or how to get it recognized by typescript in the file you need to use it.)

I don't need intelisense. I just want Sfdc to be seen as an any so that the Typescript will compile when I do something like this:

Sfdc.canvas.client.refreshSignedRequest( .....

So here is my question:

How can I make the simplest Typescript Definition file (.d.ts) possible for a JavaScript file with a bunch of functions?

And how do I get it "connected" to the JavaScript (canvas-all.js) file (that has no modules) and imported into my Typescript (myApplicationFile.ts) file?

1 Answer 1

3

OK, so this is what worked. I made a file called canvas-all.d.ts and put this in it:

declare var Sfdc: any;

After a re-build the errors went away. I had tried this before, but without a full rebuild. Visual Studio did not see it without the rebuild.

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

1 Comment

I have the same problem. but just creating the file didn't help me. do you import the Sfdc in any place in your code?

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.