-1

i am learning how to declare app-wide globla constatnts. referring to this post here for the answer of Dominic Barnes, for the code titled with constants.js -- Better. i copied it and pasted it as shown in the following lines:

constants.js

function define(name, value) {
    Object.defineProperty(exports, name, {
        value:      value,
        enumerable: true
    });
}

define("PI", 3.14);

invocation to constants.js in app.js

import constants from './constants/constants';
var req=https.get('https://xx/xx/getDistanceFromOriginToDestination/30.976639,30.056021,30.97775,30.033333',options, callback);
constants. //<==========????
req.on('error', (e) => {
    console.error("problem with request:", e);
});
req.end();

the propblem is, in the post of the Dominic Barnes he can access the PI as follows and as shown in his answer:

var constants = require("./constants");
console.log(constants.PI); // 3.14
constants.PI = 5;
console.log(constants.PI); // still 3.14

in my case, the autocomplete of VS-Codes does not show any thing derived from constants.js, or in other words, i can not have access to PI. please let me know what i am missing to chieve accessing the constant PI declared in constants.js

Amrmsmb
  • 1
  • 27
  • 104
  • 226
  • 2
    _"in other words, i can not have access to `PI`"_... not having intellisense for `PI` doesn't mean you can't use it. If you want VSCode to suggest it, you'll need a `.d.ts` file for the `constants` module – Phil Jun 23 '23 at 05:06
  • @Philisonstrike what is .d.ts please? – Amrmsmb Jun 23 '23 at 05:06
  • [About "*.d.ts" in TypeScript](https://stackoverflow.com/a/21247316/283366) – Phil Jun 23 '23 at 05:10

1 Answers1

1

VSCode uses Typescript type information in order to provide intellisense like auto-completion and argument hints.

You can create your own module type descriptor for the constants JavaScript module. Add a new constants.d.ts file along-side constants.js with the following contents

export declare const PI: number;

You can see the results in the below CodeSandbox example

Edit ecstatic-lovelace-c8q5p2

Without the .d.ts descriptor, the editor does not know about constants.PI

no type hints

But as soon as the descriptor is enabled, you get auto-completion and type hints

with type hints

Note that in either case, the code compiles and displays the correct value.

Phil
  • 157,677
  • 23
  • 242
  • 245
  • but i would like to use the same style as mentioned in constants.js please.should i just rename it from constants.js to constants.d.ts? – Amrmsmb Jun 23 '23 at 05:49
  • You don't need to change anything about your current code. The `constants.d.ts` is a **new** file – Phil Jun 23 '23 at 05:49
  • sorry but i would like to use the same notation mentioned in constants.js?!! – Amrmsmb Jun 23 '23 at 05:53
  • I really don't know what you're asking at this stage. There is absolutely nothing stopping you using `constants.PI` in your current code. VSCode won't give you any auto-completion but your code will compile and work correctly. If you _do_ want intellisense, all you have to do is create this one new `constants.d.ts` file with the content from my answer and VSCode **will** give you the auto-completion you're after – Phil Jun 23 '23 at 06:06
  • can the file titled .d.ts as the one you provided, contains the code mentioned in constants.js posted in my question?so sorry for the lake of understanding – Amrmsmb Jun 23 '23 at 06:25
  • No. It's just a type descriptor file. It has no runnable code – Phil Jun 23 '23 at 06:26
  • ok, then the file wit ext .d.ts is not relevant anyhow to the constants.js?if yes, then i can use either of the them..right? – Amrmsmb Jun 23 '23 at 06:27
  • Sorry, you've lost me again. The `.d.ts` file is literally only providing information for your editor (VSCode) to use. – Phil Jun 23 '23 at 06:31
  • :) i got you..but then what is the purpose or the need of constants.js if i am gonna use the one with ext .d.ts please – Amrmsmb Jun 23 '23 at 06:34
  • _"i got you"_... no, I don't think you do. `constants.js` has runnable, usable code; it is your **implementation**. `constants.d.ts` is just a descriptor file that describes the types pertaining to `constants.js` so your editor works nicer. – Phil Jun 23 '23 at 06:37
  • sure.and that mean, constants.d.ts can not use the notation of constants.js..right? – Amrmsmb Jun 23 '23 at 06:41
  • 1
    @LetsamrIt there may be tools that can generate `.d.ts` files from `.js` files but in this case, I'd be surprised if they worked correctly. – Phil Jun 23 '23 at 06:46