How do you add polyfills to Globals in typescript (modules)

后端 未结 2 631
深忆病人
深忆病人 2021-01-18 00:39

I was able to find a polyfill(on stack overflow) for Array#includes and add it to typescript but after adding a small import to my file it turned into a module(I understand

相关标签:
2条回答
  • 2021-01-18 01:05

    You need to declare it in the global namespace:

    declare global {
      interface Array<T> {
          includes(searchElement: T) : boolean;
      }
    }
    

    More

    Extending lib.d.ts is covered here : https://basarat.gitbooks.io/typescript/content/docs/types/lib.d.ts.html

    0 讨论(0)
  • 2021-01-18 01:23

    Besides adding a declaration, you need to include this polyfill in your code. One way to do that is by importing the file with poylfill declaration and implementation in the entry point module of your application as long as you're polyfilling core objects and want them to be available anywhere in your app (assuming you have your polyfills in ./polyfill/arrayIncludes.ts) like this:

    import './polyfill/arrayIncludes';
    
    0 讨论(0)
提交回复
热议问题