Typescript: is it possible to expose global variables using `declare const` without module












0















I'd like to have a globally available variable (without import) User to I can later do User.findById() in any file.



I have a User.ts file with:

interface UserInstance{
...
}
interface UserClass {
findById(id:number):UserInstance
...
}
declare const User: UserClass



I was expecting that in another file (let's say UserService.ts), I would be able to do User.findById(1) (without import), but I get TS 2304: Cannot find name 'User'.



My tsconfig.json is

{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"target": "es6",
"noImplicitAny": false,
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist",
"baseUrl": ".",
"paths": {
"*": [
"node_modules/*",
"types/*"
]
}
},
"include": [
"api/**/*",
"tests/**/*"
]
}



And User.ts is in api/models so I assume that the TypeScript compiler would see it when compiling UserService.ts that is in api/services.



I am new to TypeScript, is it even possible to have global variables not coming from a module?



Thanks!










share|improve this question



























    0















    I'd like to have a globally available variable (without import) User to I can later do User.findById() in any file.



    I have a User.ts file with:

    interface UserInstance{
    ...
    }
    interface UserClass {
    findById(id:number):UserInstance
    ...
    }
    declare const User: UserClass



    I was expecting that in another file (let's say UserService.ts), I would be able to do User.findById(1) (without import), but I get TS 2304: Cannot find name 'User'.



    My tsconfig.json is

    {
    "compilerOptions": {
    "module": "commonjs",
    "esModuleInterop": true,
    "target": "es6",
    "noImplicitAny": false,
    "moduleResolution": "node",
    "sourceMap": true,
    "outDir": "dist",
    "baseUrl": ".",
    "paths": {
    "*": [
    "node_modules/*",
    "types/*"
    ]
    }
    },
    "include": [
    "api/**/*",
    "tests/**/*"
    ]
    }



    And User.ts is in api/models so I assume that the TypeScript compiler would see it when compiling UserService.ts that is in api/services.



    I am new to TypeScript, is it even possible to have global variables not coming from a module?



    Thanks!










    share|improve this question

























      0












      0








      0








      I'd like to have a globally available variable (without import) User to I can later do User.findById() in any file.



      I have a User.ts file with:

      interface UserInstance{
      ...
      }
      interface UserClass {
      findById(id:number):UserInstance
      ...
      }
      declare const User: UserClass



      I was expecting that in another file (let's say UserService.ts), I would be able to do User.findById(1) (without import), but I get TS 2304: Cannot find name 'User'.



      My tsconfig.json is

      {
      "compilerOptions": {
      "module": "commonjs",
      "esModuleInterop": true,
      "target": "es6",
      "noImplicitAny": false,
      "moduleResolution": "node",
      "sourceMap": true,
      "outDir": "dist",
      "baseUrl": ".",
      "paths": {
      "*": [
      "node_modules/*",
      "types/*"
      ]
      }
      },
      "include": [
      "api/**/*",
      "tests/**/*"
      ]
      }



      And User.ts is in api/models so I assume that the TypeScript compiler would see it when compiling UserService.ts that is in api/services.



      I am new to TypeScript, is it even possible to have global variables not coming from a module?



      Thanks!










      share|improve this question














      I'd like to have a globally available variable (without import) User to I can later do User.findById() in any file.



      I have a User.ts file with:

      interface UserInstance{
      ...
      }
      interface UserClass {
      findById(id:number):UserInstance
      ...
      }
      declare const User: UserClass



      I was expecting that in another file (let's say UserService.ts), I would be able to do User.findById(1) (without import), but I get TS 2304: Cannot find name 'User'.



      My tsconfig.json is

      {
      "compilerOptions": {
      "module": "commonjs",
      "esModuleInterop": true,
      "target": "es6",
      "noImplicitAny": false,
      "moduleResolution": "node",
      "sourceMap": true,
      "outDir": "dist",
      "baseUrl": ".",
      "paths": {
      "*": [
      "node_modules/*",
      "types/*"
      ]
      }
      },
      "include": [
      "api/**/*",
      "tests/**/*"
      ]
      }



      And User.ts is in api/models so I assume that the TypeScript compiler would see it when compiling UserService.ts that is in api/services.



      I am new to TypeScript, is it even possible to have global variables not coming from a module?



      Thanks!







      typescript






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 25 '18 at 20:26









      Manuel DarveauManuel Darveau

      3,17022133




      3,17022133
























          2 Answers
          2






          active

          oldest

          votes


















          0














          You just need to export them: export const User: UserClass and then import where you want to use it of course






          share|improve this answer
























          • Thanks, but I'd like to have User available without import.

            – Manuel Darveau
            Nov 25 '18 at 20:34











          • You could use a class like this perhaps: codepen.io/anon/pen/YRvZeq?editors=0011

            – Adam Spence
            Nov 25 '18 at 21:30



















          0














          What I was looking for is:

          declare global {
          const User: UserClass;
          }



          Also, I initially added it to a globals.d.ts in the folder types, but according to my tsconfig.json, it needed to be in a folder in the include list.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53471594%2ftypescript-is-it-possible-to-expose-global-variables-using-declare-const-with%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            You just need to export them: export const User: UserClass and then import where you want to use it of course






            share|improve this answer
























            • Thanks, but I'd like to have User available without import.

              – Manuel Darveau
              Nov 25 '18 at 20:34











            • You could use a class like this perhaps: codepen.io/anon/pen/YRvZeq?editors=0011

              – Adam Spence
              Nov 25 '18 at 21:30
















            0














            You just need to export them: export const User: UserClass and then import where you want to use it of course






            share|improve this answer
























            • Thanks, but I'd like to have User available without import.

              – Manuel Darveau
              Nov 25 '18 at 20:34











            • You could use a class like this perhaps: codepen.io/anon/pen/YRvZeq?editors=0011

              – Adam Spence
              Nov 25 '18 at 21:30














            0












            0








            0







            You just need to export them: export const User: UserClass and then import where you want to use it of course






            share|improve this answer













            You just need to export them: export const User: UserClass and then import where you want to use it of course







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 25 '18 at 20:29









            Adam SpenceAdam Spence

            1,4981413




            1,4981413













            • Thanks, but I'd like to have User available without import.

              – Manuel Darveau
              Nov 25 '18 at 20:34











            • You could use a class like this perhaps: codepen.io/anon/pen/YRvZeq?editors=0011

              – Adam Spence
              Nov 25 '18 at 21:30



















            • Thanks, but I'd like to have User available without import.

              – Manuel Darveau
              Nov 25 '18 at 20:34











            • You could use a class like this perhaps: codepen.io/anon/pen/YRvZeq?editors=0011

              – Adam Spence
              Nov 25 '18 at 21:30

















            Thanks, but I'd like to have User available without import.

            – Manuel Darveau
            Nov 25 '18 at 20:34





            Thanks, but I'd like to have User available without import.

            – Manuel Darveau
            Nov 25 '18 at 20:34













            You could use a class like this perhaps: codepen.io/anon/pen/YRvZeq?editors=0011

            – Adam Spence
            Nov 25 '18 at 21:30





            You could use a class like this perhaps: codepen.io/anon/pen/YRvZeq?editors=0011

            – Adam Spence
            Nov 25 '18 at 21:30













            0














            What I was looking for is:

            declare global {
            const User: UserClass;
            }



            Also, I initially added it to a globals.d.ts in the folder types, but according to my tsconfig.json, it needed to be in a folder in the include list.






            share|improve this answer




























              0














              What I was looking for is:

              declare global {
              const User: UserClass;
              }



              Also, I initially added it to a globals.d.ts in the folder types, but according to my tsconfig.json, it needed to be in a folder in the include list.






              share|improve this answer


























                0












                0








                0







                What I was looking for is:

                declare global {
                const User: UserClass;
                }



                Also, I initially added it to a globals.d.ts in the folder types, but according to my tsconfig.json, it needed to be in a folder in the include list.






                share|improve this answer













                What I was looking for is:

                declare global {
                const User: UserClass;
                }



                Also, I initially added it to a globals.d.ts in the folder types, but according to my tsconfig.json, it needed to be in a folder in the include list.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 26 '18 at 2:05









                Manuel DarveauManuel Darveau

                3,17022133




                3,17022133






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53471594%2ftypescript-is-it-possible-to-expose-global-variables-using-declare-const-with%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Wiesbaden

                    To store a contact into the json file from server.js file using a class in NodeJS

                    Marschland