Can we write declarations inline, inside JavaScript files, so we don't have to write extra .d.ts files?












0















If I write in plain JavaScript, I know I can currently place a sibling .d.ts file next to my .js file in order to type it for use in TypeScript.



Is it possible to write only a .js file and place the declarations in there instead of having separate .d.ts files?



I know currently we can write jsdoc-style comments, and get some typing, but it's fairly limited compared to actual TypeScript. I'd love to write normal declarations inside the .js files.










share|improve this question



























    0















    If I write in plain JavaScript, I know I can currently place a sibling .d.ts file next to my .js file in order to type it for use in TypeScript.



    Is it possible to write only a .js file and place the declarations in there instead of having separate .d.ts files?



    I know currently we can write jsdoc-style comments, and get some typing, but it's fairly limited compared to actual TypeScript. I'd love to write normal declarations inside the .js files.










    share|improve this question

























      0












      0








      0








      If I write in plain JavaScript, I know I can currently place a sibling .d.ts file next to my .js file in order to type it for use in TypeScript.



      Is it possible to write only a .js file and place the declarations in there instead of having separate .d.ts files?



      I know currently we can write jsdoc-style comments, and get some typing, but it's fairly limited compared to actual TypeScript. I'd love to write normal declarations inside the .js files.










      share|improve this question














      If I write in plain JavaScript, I know I can currently place a sibling .d.ts file next to my .js file in order to type it for use in TypeScript.



      Is it possible to write only a .js file and place the declarations in there instead of having separate .d.ts files?



      I know currently we can write jsdoc-style comments, and get some typing, but it's fairly limited compared to actual TypeScript. I'd love to write normal declarations inside the .js files.







      typescript type-declaration type-definition






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 1 at 3:50









      trusktrtrusktr

      17.1k31116162




      17.1k31116162
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Although the TypeScript is considering it, it's not possible as of now. Only *.ts files offer the full power of TypeScript.



          You could, hypothetically, define all your modules in a single declaration file, but at this point, it will probably be easier to just use TypeScript instead of JavaScript.



          If that's beyond your control, the only thing that comes to mind is creating a custom transformer with TypeScript API. Since type definitions are not valid JavaScript and using them inside a .js file would cause a syntax error, you would still need to put them in a comment.






          share|improve this answer


























          • It's not out of my control, but I can do things in JS that are very hard to do in TypeScript. F.e., I'm still learning how to solve this one: stackoverflow.com/questions/50899400. I want to generate classes with private and protected members based on object literals. My package "lowclass" does this for runtime JS: github.com/trusktr/lowclass. So I thought, instead of trying to type the class factory in order to use Class() in a TS project, maybe I could just use Class() and then write simple type defs, as writing a class with private and protected members is easy.

            – trusktr
            Jan 2 at 6:42











          • In short, arguably figuring out how to do stackoverflow.com/questions/50899400 (with additional private and protected members) seems more difficult than just writing some JS with a simple accompanying class declaration. If they could live in the same file that'd be sweet!

            – trusktr
            Jan 2 at 6:49











          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%2f53992901%2fcan-we-write-declarations-inline-inside-javascript-files-so-we-dont-have-to-w%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Although the TypeScript is considering it, it's not possible as of now. Only *.ts files offer the full power of TypeScript.



          You could, hypothetically, define all your modules in a single declaration file, but at this point, it will probably be easier to just use TypeScript instead of JavaScript.



          If that's beyond your control, the only thing that comes to mind is creating a custom transformer with TypeScript API. Since type definitions are not valid JavaScript and using them inside a .js file would cause a syntax error, you would still need to put them in a comment.






          share|improve this answer


























          • It's not out of my control, but I can do things in JS that are very hard to do in TypeScript. F.e., I'm still learning how to solve this one: stackoverflow.com/questions/50899400. I want to generate classes with private and protected members based on object literals. My package "lowclass" does this for runtime JS: github.com/trusktr/lowclass. So I thought, instead of trying to type the class factory in order to use Class() in a TS project, maybe I could just use Class() and then write simple type defs, as writing a class with private and protected members is easy.

            – trusktr
            Jan 2 at 6:42











          • In short, arguably figuring out how to do stackoverflow.com/questions/50899400 (with additional private and protected members) seems more difficult than just writing some JS with a simple accompanying class declaration. If they could live in the same file that'd be sweet!

            – trusktr
            Jan 2 at 6:49
















          1














          Although the TypeScript is considering it, it's not possible as of now. Only *.ts files offer the full power of TypeScript.



          You could, hypothetically, define all your modules in a single declaration file, but at this point, it will probably be easier to just use TypeScript instead of JavaScript.



          If that's beyond your control, the only thing that comes to mind is creating a custom transformer with TypeScript API. Since type definitions are not valid JavaScript and using them inside a .js file would cause a syntax error, you would still need to put them in a comment.






          share|improve this answer


























          • It's not out of my control, but I can do things in JS that are very hard to do in TypeScript. F.e., I'm still learning how to solve this one: stackoverflow.com/questions/50899400. I want to generate classes with private and protected members based on object literals. My package "lowclass" does this for runtime JS: github.com/trusktr/lowclass. So I thought, instead of trying to type the class factory in order to use Class() in a TS project, maybe I could just use Class() and then write simple type defs, as writing a class with private and protected members is easy.

            – trusktr
            Jan 2 at 6:42











          • In short, arguably figuring out how to do stackoverflow.com/questions/50899400 (with additional private and protected members) seems more difficult than just writing some JS with a simple accompanying class declaration. If they could live in the same file that'd be sweet!

            – trusktr
            Jan 2 at 6:49














          1












          1








          1







          Although the TypeScript is considering it, it's not possible as of now. Only *.ts files offer the full power of TypeScript.



          You could, hypothetically, define all your modules in a single declaration file, but at this point, it will probably be easier to just use TypeScript instead of JavaScript.



          If that's beyond your control, the only thing that comes to mind is creating a custom transformer with TypeScript API. Since type definitions are not valid JavaScript and using them inside a .js file would cause a syntax error, you would still need to put them in a comment.






          share|improve this answer















          Although the TypeScript is considering it, it's not possible as of now. Only *.ts files offer the full power of TypeScript.



          You could, hypothetically, define all your modules in a single declaration file, but at this point, it will probably be easier to just use TypeScript instead of JavaScript.



          If that's beyond your control, the only thing that comes to mind is creating a custom transformer with TypeScript API. Since type definitions are not valid JavaScript and using them inside a .js file would cause a syntax error, you would still need to put them in a comment.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 4 at 7:38

























          answered Jan 1 at 4:01









          Karol MajewskiKarol Majewski

          3,491213




          3,491213













          • It's not out of my control, but I can do things in JS that are very hard to do in TypeScript. F.e., I'm still learning how to solve this one: stackoverflow.com/questions/50899400. I want to generate classes with private and protected members based on object literals. My package "lowclass" does this for runtime JS: github.com/trusktr/lowclass. So I thought, instead of trying to type the class factory in order to use Class() in a TS project, maybe I could just use Class() and then write simple type defs, as writing a class with private and protected members is easy.

            – trusktr
            Jan 2 at 6:42











          • In short, arguably figuring out how to do stackoverflow.com/questions/50899400 (with additional private and protected members) seems more difficult than just writing some JS with a simple accompanying class declaration. If they could live in the same file that'd be sweet!

            – trusktr
            Jan 2 at 6:49



















          • It's not out of my control, but I can do things in JS that are very hard to do in TypeScript. F.e., I'm still learning how to solve this one: stackoverflow.com/questions/50899400. I want to generate classes with private and protected members based on object literals. My package "lowclass" does this for runtime JS: github.com/trusktr/lowclass. So I thought, instead of trying to type the class factory in order to use Class() in a TS project, maybe I could just use Class() and then write simple type defs, as writing a class with private and protected members is easy.

            – trusktr
            Jan 2 at 6:42











          • In short, arguably figuring out how to do stackoverflow.com/questions/50899400 (with additional private and protected members) seems more difficult than just writing some JS with a simple accompanying class declaration. If they could live in the same file that'd be sweet!

            – trusktr
            Jan 2 at 6:49

















          It's not out of my control, but I can do things in JS that are very hard to do in TypeScript. F.e., I'm still learning how to solve this one: stackoverflow.com/questions/50899400. I want to generate classes with private and protected members based on object literals. My package "lowclass" does this for runtime JS: github.com/trusktr/lowclass. So I thought, instead of trying to type the class factory in order to use Class() in a TS project, maybe I could just use Class() and then write simple type defs, as writing a class with private and protected members is easy.

          – trusktr
          Jan 2 at 6:42





          It's not out of my control, but I can do things in JS that are very hard to do in TypeScript. F.e., I'm still learning how to solve this one: stackoverflow.com/questions/50899400. I want to generate classes with private and protected members based on object literals. My package "lowclass" does this for runtime JS: github.com/trusktr/lowclass. So I thought, instead of trying to type the class factory in order to use Class() in a TS project, maybe I could just use Class() and then write simple type defs, as writing a class with private and protected members is easy.

          – trusktr
          Jan 2 at 6:42













          In short, arguably figuring out how to do stackoverflow.com/questions/50899400 (with additional private and protected members) seems more difficult than just writing some JS with a simple accompanying class declaration. If they could live in the same file that'd be sweet!

          – trusktr
          Jan 2 at 6:49





          In short, arguably figuring out how to do stackoverflow.com/questions/50899400 (with additional private and protected members) seems more difficult than just writing some JS with a simple accompanying class declaration. If they could live in the same file that'd be sweet!

          – trusktr
          Jan 2 at 6:49




















          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%2f53992901%2fcan-we-write-declarations-inline-inside-javascript-files-so-we-dont-have-to-w%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

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas