How to create a definition typescript file with a default export and functions inside?












0















I have to use an internal library to authenticate the users, but now I have to use this lib in a typescript project, so I'm trying to create its definition file (.d.ts), but none of I've tried works.



Normally I would use it like this:



import login from 'int-login';
...
login.requestAuth('admin', 'admin').then(user => console.log(user));


So I tried to create the definition file like this:



declare module 'int-login' {
export default class login {
requestAuth(username: string, password: string);
}
}


But this is the error returned: Property 'requestAuth' does not exist on type 'typeof login'.
I tried to move the function out of the class 'login', but then it says login doesn't exists whe I import it in my typescript file.










share|improve this question



























    0















    I have to use an internal library to authenticate the users, but now I have to use this lib in a typescript project, so I'm trying to create its definition file (.d.ts), but none of I've tried works.



    Normally I would use it like this:



    import login from 'int-login';
    ...
    login.requestAuth('admin', 'admin').then(user => console.log(user));


    So I tried to create the definition file like this:



    declare module 'int-login' {
    export default class login {
    requestAuth(username: string, password: string);
    }
    }


    But this is the error returned: Property 'requestAuth' does not exist on type 'typeof login'.
    I tried to move the function out of the class 'login', but then it says login doesn't exists whe I import it in my typescript file.










    share|improve this question

























      0












      0








      0








      I have to use an internal library to authenticate the users, but now I have to use this lib in a typescript project, so I'm trying to create its definition file (.d.ts), but none of I've tried works.



      Normally I would use it like this:



      import login from 'int-login';
      ...
      login.requestAuth('admin', 'admin').then(user => console.log(user));


      So I tried to create the definition file like this:



      declare module 'int-login' {
      export default class login {
      requestAuth(username: string, password: string);
      }
      }


      But this is the error returned: Property 'requestAuth' does not exist on type 'typeof login'.
      I tried to move the function out of the class 'login', but then it says login doesn't exists whe I import it in my typescript file.










      share|improve this question














      I have to use an internal library to authenticate the users, but now I have to use this lib in a typescript project, so I'm trying to create its definition file (.d.ts), but none of I've tried works.



      Normally I would use it like this:



      import login from 'int-login';
      ...
      login.requestAuth('admin', 'admin').then(user => console.log(user));


      So I tried to create the definition file like this:



      declare module 'int-login' {
      export default class login {
      requestAuth(username: string, password: string);
      }
      }


      But this is the error returned: Property 'requestAuth' does not exist on type 'typeof login'.
      I tried to move the function out of the class 'login', but then it says login doesn't exists whe I import it in my typescript file.







      typescript typescript-definitions






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 2 at 16:55









      ImacImac

      1039




      1039
























          1 Answer
          1






          active

          oldest

          votes


















          0














          The format for the definition file depends on whether the definition file is packaged with the library, or is in the project that depends on the library. If you want to package the definitions with the library then remove the declare module wrapper, and make sure that there is a "types" property in the library package.json file with the location of the definition file.



          It looks like you expect requestAuth to return a promise, so you need to indicate the return type in the definitions.



          The definition file should have this content.



          export interface User {
          // list appropriate property types here
          }

          export default class login {
          requestAuth(username: string, password: string): Promise<User>;
          }


          On the other hand if the definition file is in the project that depends on int-login (perhaps in a types/ folder), or in a separate @types/int-login package, then you do need the declare module wrapping. You also need to make sure that the definition file is included by the "include" configuration value in tsconfig.json. In that case the definition file would look like this:



          declare module "int-login" {
          export interface User {
          // list appropriate property types here
          }

          export default class login {
          requestAuth(username: string, password: string): Promise<User>;
          }
          }





          share|improve this answer
























          • Yes, I need the module wrapping, but when I do like your example, I get this error "Property 'requestAuth' does not exist on type 'typeof login'"

            – Imac
            Jan 7 at 11:55












          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%2f54010227%2fhow-to-create-a-definition-typescript-file-with-a-default-export-and-functions-i%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









          0














          The format for the definition file depends on whether the definition file is packaged with the library, or is in the project that depends on the library. If you want to package the definitions with the library then remove the declare module wrapper, and make sure that there is a "types" property in the library package.json file with the location of the definition file.



          It looks like you expect requestAuth to return a promise, so you need to indicate the return type in the definitions.



          The definition file should have this content.



          export interface User {
          // list appropriate property types here
          }

          export default class login {
          requestAuth(username: string, password: string): Promise<User>;
          }


          On the other hand if the definition file is in the project that depends on int-login (perhaps in a types/ folder), or in a separate @types/int-login package, then you do need the declare module wrapping. You also need to make sure that the definition file is included by the "include" configuration value in tsconfig.json. In that case the definition file would look like this:



          declare module "int-login" {
          export interface User {
          // list appropriate property types here
          }

          export default class login {
          requestAuth(username: string, password: string): Promise<User>;
          }
          }





          share|improve this answer
























          • Yes, I need the module wrapping, but when I do like your example, I get this error "Property 'requestAuth' does not exist on type 'typeof login'"

            – Imac
            Jan 7 at 11:55
















          0














          The format for the definition file depends on whether the definition file is packaged with the library, or is in the project that depends on the library. If you want to package the definitions with the library then remove the declare module wrapper, and make sure that there is a "types" property in the library package.json file with the location of the definition file.



          It looks like you expect requestAuth to return a promise, so you need to indicate the return type in the definitions.



          The definition file should have this content.



          export interface User {
          // list appropriate property types here
          }

          export default class login {
          requestAuth(username: string, password: string): Promise<User>;
          }


          On the other hand if the definition file is in the project that depends on int-login (perhaps in a types/ folder), or in a separate @types/int-login package, then you do need the declare module wrapping. You also need to make sure that the definition file is included by the "include" configuration value in tsconfig.json. In that case the definition file would look like this:



          declare module "int-login" {
          export interface User {
          // list appropriate property types here
          }

          export default class login {
          requestAuth(username: string, password: string): Promise<User>;
          }
          }





          share|improve this answer
























          • Yes, I need the module wrapping, but when I do like your example, I get this error "Property 'requestAuth' does not exist on type 'typeof login'"

            – Imac
            Jan 7 at 11:55














          0












          0








          0







          The format for the definition file depends on whether the definition file is packaged with the library, or is in the project that depends on the library. If you want to package the definitions with the library then remove the declare module wrapper, and make sure that there is a "types" property in the library package.json file with the location of the definition file.



          It looks like you expect requestAuth to return a promise, so you need to indicate the return type in the definitions.



          The definition file should have this content.



          export interface User {
          // list appropriate property types here
          }

          export default class login {
          requestAuth(username: string, password: string): Promise<User>;
          }


          On the other hand if the definition file is in the project that depends on int-login (perhaps in a types/ folder), or in a separate @types/int-login package, then you do need the declare module wrapping. You also need to make sure that the definition file is included by the "include" configuration value in tsconfig.json. In that case the definition file would look like this:



          declare module "int-login" {
          export interface User {
          // list appropriate property types here
          }

          export default class login {
          requestAuth(username: string, password: string): Promise<User>;
          }
          }





          share|improve this answer













          The format for the definition file depends on whether the definition file is packaged with the library, or is in the project that depends on the library. If you want to package the definitions with the library then remove the declare module wrapper, and make sure that there is a "types" property in the library package.json file with the location of the definition file.



          It looks like you expect requestAuth to return a promise, so you need to indicate the return type in the definitions.



          The definition file should have this content.



          export interface User {
          // list appropriate property types here
          }

          export default class login {
          requestAuth(username: string, password: string): Promise<User>;
          }


          On the other hand if the definition file is in the project that depends on int-login (perhaps in a types/ folder), or in a separate @types/int-login package, then you do need the declare module wrapping. You also need to make sure that the definition file is included by the "include" configuration value in tsconfig.json. In that case the definition file would look like this:



          declare module "int-login" {
          export interface User {
          // list appropriate property types here
          }

          export default class login {
          requestAuth(username: string, password: string): Promise<User>;
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 2 at 17:55









          Jesse HallettJesse Hallett

          1,2881321




          1,2881321













          • Yes, I need the module wrapping, but when I do like your example, I get this error "Property 'requestAuth' does not exist on type 'typeof login'"

            – Imac
            Jan 7 at 11:55



















          • Yes, I need the module wrapping, but when I do like your example, I get this error "Property 'requestAuth' does not exist on type 'typeof login'"

            – Imac
            Jan 7 at 11:55

















          Yes, I need the module wrapping, but when I do like your example, I get this error "Property 'requestAuth' does not exist on type 'typeof login'"

          – Imac
          Jan 7 at 11:55





          Yes, I need the module wrapping, but when I do like your example, I get this error "Property 'requestAuth' does not exist on type 'typeof login'"

          – Imac
          Jan 7 at 11:55




















          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%2f54010227%2fhow-to-create-a-definition-typescript-file-with-a-default-export-and-functions-i%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

          Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

          Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

          A Topological Invariant for $pi_3(U(n))$