Aniruddha Das Aniruddha Das - 1 year ago 238
HTML Question

typescript Triple-Slash Directives reference to html file

Is there a way I can reference JavaScript variables defined in html file tag in my type script class?

I am developing an application in angular2 with typescript which need some plan JavaScript variables which are declared and assigned in index.html in . Wehn I am trying to reference them in my typescript class, I am getting error as below.

without reference getting below error:

app/services/Resources.ts(8,41): error TS2339: Property 'Resources' does not exist on type 'Window'.

When I try to reference index.html it show below error

/// <reference path="../../index.html" />

app/services/Resources.ts(1,1): error TS6054: File 'C:/quickstart/index.html' has unsupported extension. The only supported extensions are '
.ts', '.tsx', '.d.ts'.

Answer Source

No, you can't.

But what you can do is to declare those variables in a separate .d.ts file and then reference that:

// AddedVariables.d.ts
interface Window {
    Resources: any[];

Then simply reference that:

/// <reference path="AddedVariables.d.ts" />

And then you should be able to use Resources.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download