Typescript types for code that is not imported by typescript?









up vote
4
down vote

favorite












This is an issue with an Angular app, but I don't believe this is an angular-specific issue.



I'm working on an Angular 7 app, and I'm working with a few libraries that are imported via script tags on the front end from other sources (Stripe, recaptcha, googletags, etc). I'll use Stripe as an example here, because Stripe absolutely requires that the front end library be imported from Stripe.js by the client for PCI compliance, so from source server side is not an option.



I've installed the types for Stripe from DefinitelyTyped. If I add declare let Stripe: any; and use Stripe in a component, it works, but of course it is not type safe. If I omit the declaration, VSCode finds the types fine, and the editor catches type errors, but it won't compile, (error TS2304: Cannot find name 'Stripe'. ) presumable because I have no import statement for Stripe.



Is there any way to hint to TypeScript that it should use the types from DefinitelyTyped to catch type errors, without importing the Stripe library itself?



EDIT:
OK, I think I've figured out what specifically leads to the problem. This may be expected behavior:



It fails to compile with:



class Foo 
constructor()
bar()
console.log(Stripe);




But it works with:



class Foo 
constructor()
bar = () =>
console.log(Stripe);




It seems to be related to scope, but I'm not sure I understand exactly why this would be.



Edit again: I take back what I said about not thinking this is Angular specific. I now think this might have something to do with the angular compilation process.










share|improve this question



























    up vote
    4
    down vote

    favorite












    This is an issue with an Angular app, but I don't believe this is an angular-specific issue.



    I'm working on an Angular 7 app, and I'm working with a few libraries that are imported via script tags on the front end from other sources (Stripe, recaptcha, googletags, etc). I'll use Stripe as an example here, because Stripe absolutely requires that the front end library be imported from Stripe.js by the client for PCI compliance, so from source server side is not an option.



    I've installed the types for Stripe from DefinitelyTyped. If I add declare let Stripe: any; and use Stripe in a component, it works, but of course it is not type safe. If I omit the declaration, VSCode finds the types fine, and the editor catches type errors, but it won't compile, (error TS2304: Cannot find name 'Stripe'. ) presumable because I have no import statement for Stripe.



    Is there any way to hint to TypeScript that it should use the types from DefinitelyTyped to catch type errors, without importing the Stripe library itself?



    EDIT:
    OK, I think I've figured out what specifically leads to the problem. This may be expected behavior:



    It fails to compile with:



    class Foo 
    constructor()
    bar()
    console.log(Stripe);




    But it works with:



    class Foo 
    constructor()
    bar = () =>
    console.log(Stripe);




    It seems to be related to scope, but I'm not sure I understand exactly why this would be.



    Edit again: I take back what I said about not thinking this is Angular specific. I now think this might have something to do with the angular compilation process.










    share|improve this question

























      up vote
      4
      down vote

      favorite









      up vote
      4
      down vote

      favorite











      This is an issue with an Angular app, but I don't believe this is an angular-specific issue.



      I'm working on an Angular 7 app, and I'm working with a few libraries that are imported via script tags on the front end from other sources (Stripe, recaptcha, googletags, etc). I'll use Stripe as an example here, because Stripe absolutely requires that the front end library be imported from Stripe.js by the client for PCI compliance, so from source server side is not an option.



      I've installed the types for Stripe from DefinitelyTyped. If I add declare let Stripe: any; and use Stripe in a component, it works, but of course it is not type safe. If I omit the declaration, VSCode finds the types fine, and the editor catches type errors, but it won't compile, (error TS2304: Cannot find name 'Stripe'. ) presumable because I have no import statement for Stripe.



      Is there any way to hint to TypeScript that it should use the types from DefinitelyTyped to catch type errors, without importing the Stripe library itself?



      EDIT:
      OK, I think I've figured out what specifically leads to the problem. This may be expected behavior:



      It fails to compile with:



      class Foo 
      constructor()
      bar()
      console.log(Stripe);




      But it works with:



      class Foo 
      constructor()
      bar = () =>
      console.log(Stripe);




      It seems to be related to scope, but I'm not sure I understand exactly why this would be.



      Edit again: I take back what I said about not thinking this is Angular specific. I now think this might have something to do with the angular compilation process.










      share|improve this question















      This is an issue with an Angular app, but I don't believe this is an angular-specific issue.



      I'm working on an Angular 7 app, and I'm working with a few libraries that are imported via script tags on the front end from other sources (Stripe, recaptcha, googletags, etc). I'll use Stripe as an example here, because Stripe absolutely requires that the front end library be imported from Stripe.js by the client for PCI compliance, so from source server side is not an option.



      I've installed the types for Stripe from DefinitelyTyped. If I add declare let Stripe: any; and use Stripe in a component, it works, but of course it is not type safe. If I omit the declaration, VSCode finds the types fine, and the editor catches type errors, but it won't compile, (error TS2304: Cannot find name 'Stripe'. ) presumable because I have no import statement for Stripe.



      Is there any way to hint to TypeScript that it should use the types from DefinitelyTyped to catch type errors, without importing the Stripe library itself?



      EDIT:
      OK, I think I've figured out what specifically leads to the problem. This may be expected behavior:



      It fails to compile with:



      class Foo 
      constructor()
      bar()
      console.log(Stripe);




      But it works with:



      class Foo 
      constructor()
      bar = () =>
      console.log(Stripe);




      It seems to be related to scope, but I'm not sure I understand exactly why this would be.



      Edit again: I take back what I said about not thinking this is Angular specific. I now think this might have something to do with the angular compilation process.







      angular typescript angular7






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 at 5:21









      Cœur

      17.1k9102140




      17.1k9102140










      asked Nov 7 at 22:53









      Karptonite

      7231722




      7231722






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          I had the same problem in the past, and I also find it impossible to import the Type Definition files using import without importing the actual module.



          So, what I did is, I copied the Type Definition file (usually index.d.ts) into my codebase.



          After that, you could do something like:



          import SomeType from "sometype.d.ts"
          declare let ObjectImportedFromHTMLScriptTag: SomeType;


          Then, you could use the imported object without breaking type safety.






          share|improve this answer




















          • Not a good idea to copy past but how it works if you direct access to this d.ts from node_modules ?
            – Gilsdav
            Nov 8 at 8:04










          • @Gilsdav It will work as well without importing the actual module, however please remember to install the typings using --save-dev options.
            – Wong Jia Hau
            Nov 8 at 8:19










          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',
          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%2f53199133%2ftypescript-types-for-code-that-is-not-imported-by-typescript%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








          up vote
          0
          down vote













          I had the same problem in the past, and I also find it impossible to import the Type Definition files using import without importing the actual module.



          So, what I did is, I copied the Type Definition file (usually index.d.ts) into my codebase.



          After that, you could do something like:



          import SomeType from "sometype.d.ts"
          declare let ObjectImportedFromHTMLScriptTag: SomeType;


          Then, you could use the imported object without breaking type safety.






          share|improve this answer




















          • Not a good idea to copy past but how it works if you direct access to this d.ts from node_modules ?
            – Gilsdav
            Nov 8 at 8:04










          • @Gilsdav It will work as well without importing the actual module, however please remember to install the typings using --save-dev options.
            – Wong Jia Hau
            Nov 8 at 8:19














          up vote
          0
          down vote













          I had the same problem in the past, and I also find it impossible to import the Type Definition files using import without importing the actual module.



          So, what I did is, I copied the Type Definition file (usually index.d.ts) into my codebase.



          After that, you could do something like:



          import SomeType from "sometype.d.ts"
          declare let ObjectImportedFromHTMLScriptTag: SomeType;


          Then, you could use the imported object without breaking type safety.






          share|improve this answer




















          • Not a good idea to copy past but how it works if you direct access to this d.ts from node_modules ?
            – Gilsdav
            Nov 8 at 8:04










          • @Gilsdav It will work as well without importing the actual module, however please remember to install the typings using --save-dev options.
            – Wong Jia Hau
            Nov 8 at 8:19












          up vote
          0
          down vote










          up vote
          0
          down vote









          I had the same problem in the past, and I also find it impossible to import the Type Definition files using import without importing the actual module.



          So, what I did is, I copied the Type Definition file (usually index.d.ts) into my codebase.



          After that, you could do something like:



          import SomeType from "sometype.d.ts"
          declare let ObjectImportedFromHTMLScriptTag: SomeType;


          Then, you could use the imported object without breaking type safety.






          share|improve this answer












          I had the same problem in the past, and I also find it impossible to import the Type Definition files using import without importing the actual module.



          So, what I did is, I copied the Type Definition file (usually index.d.ts) into my codebase.



          After that, you could do something like:



          import SomeType from "sometype.d.ts"
          declare let ObjectImportedFromHTMLScriptTag: SomeType;


          Then, you could use the imported object without breaking type safety.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 8 at 1:27









          Wong Jia Hau

          42448




          42448











          • Not a good idea to copy past but how it works if you direct access to this d.ts from node_modules ?
            – Gilsdav
            Nov 8 at 8:04










          • @Gilsdav It will work as well without importing the actual module, however please remember to install the typings using --save-dev options.
            – Wong Jia Hau
            Nov 8 at 8:19
















          • Not a good idea to copy past but how it works if you direct access to this d.ts from node_modules ?
            – Gilsdav
            Nov 8 at 8:04










          • @Gilsdav It will work as well without importing the actual module, however please remember to install the typings using --save-dev options.
            – Wong Jia Hau
            Nov 8 at 8:19















          Not a good idea to copy past but how it works if you direct access to this d.ts from node_modules ?
          – Gilsdav
          Nov 8 at 8:04




          Not a good idea to copy past but how it works if you direct access to this d.ts from node_modules ?
          – Gilsdav
          Nov 8 at 8:04












          @Gilsdav It will work as well without importing the actual module, however please remember to install the typings using --save-dev options.
          – Wong Jia Hau
          Nov 8 at 8:19




          @Gilsdav It will work as well without importing the actual module, however please remember to install the typings using --save-dev options.
          – Wong Jia Hau
          Nov 8 at 8:19

















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53199133%2ftypescript-types-for-code-that-is-not-imported-by-typescript%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

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo