0

I have been developing in JavaScript a long time ago. Lately I was asked to do some work with client side scripting so I decided to look into TypeScript, to avoid some of JavaScript pitfalls. My problem is probably basic but I could not find an answer that would be clear to me with my lack of TypeScript / new JavaScript development over the last 10 years. Here it is - I have three files -

1. index.html
2. Globals.ts
3. Drawings.ts

Index.html is the file to be served, in it a JavaScript will arrange and interact with the user. Globals.ts - simply contain my global variables / constants Drawing.ts - contain the Drawing functions for the data, it is referencing globals.ts for the global definitions, with this statement /// <reference path="globals.ts"/>.

In index.html I use the SCRIPT tag with src='Drawings.ts' right under the HEAD Tag.

<SCRIPT lang='JavaScript' src='Drawing.ts'></SCRIPT>
<SCRIPT lang='JavaScript'>local code goes here</SCRIPT>

All the globals in the Global.ts are declared with the word var and assigned a value, like this -

    var x_scale_h_offset = 15;
    var y_scale_v_offset  = 15;
    var g_general_margin  = 10;
    var g_std_font_height  = 10;
    var dpi = 1;

After checking for document complete status I call the init_d() function from Drawings.ts which looks like this -

function init_d()
{
    dpi = window.devicePixelRatio;
}

I get a runtime Uncaught exception dpi is not defined.

I was under the impression that variables declared in a file which are not part of an object are global to all scripts. I also assume - which I might be wrong - that this reference statement /// <reference path="globals.ts"/> will cause Globals.ts to be loaded.

Can someone explain - in simplicity - what am I doing wrong and how to fix it?

Regards

  • You will likely need to build the TypeScript (.ts) to vanilla JavaScript (.js) as there are likely some TS specific things that the browser doesn't support like the reference path – phuzi Dec 30 '21 at 13:11
  • Why would you want to serve TS files? Also why use the ancient `lang='JavaScript'` ? I would expect `` – mplungjan Dec 30 '21 at 13:12
  • But you can't do ` – Jeremy Thille Dec 30 '21 at 13:12
  • It seems that the browser find the Drawings.ts, as I do get to the init_d function that resides there, but it seems that the globals.ts is where the problem occurs, as the dpi is declared there and I get runtime error on it not being recognaized. ```Why would you want to serve TS files? Also why use the ancient lang='JavaScript' ? I would expect ``` I am not familiar with this syntax, like I said I guess I'm missing 10 years of changes and development of JavaScript. – Ophir Bibi Dec 30 '21 at 13:58

0 Answers0