最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Typescript - How to avoid compilation error from undeclared objects - Stack Overflow

programmeradmin2浏览0评论

Assuming I have an external javascript inserted in the page and it exports a few things to the external scope (ie binds them to window).

I want to call some of those properties in my typescript projects as:

UndeclaredExportedProperty.aFunction()

But typescript will not allow me to pile that ><

I don't want to go through a convoluted way of declaring the whole interface of the module since I don't know it and quite frankly I don't care about it. Its just a module that I have to call once and "trust" that by the time I make the call its loaded and contains the correct elements (its non critical so not calling it will not make the world catch fire).

What is the easiest way of doing this with typescript ?

Edit in response to mark as duplicate:

Whilst the answer to that question did solve my problem the question is different (for example stack isn't able to find it as a duplicate suggestion) and I feel like for what I'm trying to do Pokus answer is a more straight forward and general solutions than the answers in that question

That being said, if an admin feels like this is still a duplicate or to simple of a question feel free to delete/close since I've gotten my answer. Personally I will leave it standing because the next person searching via google or so might find an answer more easily this way.

Assuming I have an external javascript inserted in the page and it exports a few things to the external scope (ie binds them to window).

I want to call some of those properties in my typescript projects as:

UndeclaredExportedProperty.aFunction()

But typescript will not allow me to pile that ><

I don't want to go through a convoluted way of declaring the whole interface of the module since I don't know it and quite frankly I don't care about it. Its just a module that I have to call once and "trust" that by the time I make the call its loaded and contains the correct elements (its non critical so not calling it will not make the world catch fire).

What is the easiest way of doing this with typescript ?

Edit in response to mark as duplicate:

Whilst the answer to that question did solve my problem the question is different (for example stack isn't able to find it as a duplicate suggestion) and I feel like for what I'm trying to do Pokus answer is a more straight forward and general solutions than the answers in that question

That being said, if an admin feels like this is still a duplicate or to simple of a question feel free to delete/close since I've gotten my answer. Personally I will leave it standing because the next person searching via google or so might find an answer more easily this way.

Share Improve this question edited May 21, 2017 at 20:23 George asked May 21, 2017 at 20:11 GeorgeGeorge 4,0475 gold badges36 silver badges82 bronze badges 4
  • Possible duplicate of How do you explicitly set a new property on `window` in TypeScript? – Simon Groenewolt Commented May 21, 2017 at 20:18
  • 1 @ Simon Groenewolt Whilst the answer to that question did solve my problem the question is different (for example stack isn't able to find it as a duplicate suggestion) and I feel like for what I'm trying to do Pokus answer is a more straight forward and general solutions than the answers in that question – George Commented May 21, 2017 at 20:22
  • Possible duplicate of typescript: What to do if a typings (or tsd) is not available? – Alex Commented May 21, 2017 at 20:34
  • @George check, retracted my close vote :-) – Simon Groenewolt Commented May 22, 2017 at 10:28
Add a ment  | 

3 Answers 3

Reset to default 6

You should use the declare keyword for that:

declare var UndeclaredExportedProperty: any;

The official docs states this here: https://www.typescriptlang/docs/handbook/modules.html in the section Working with Other JavaScript Libraries

Try

(window as any).UndeclaredExportedProperty.aFunction()

this will tell the piler to temporarily treat the Window instance as untyped and allow you to do anything with it.

Ok, this question was apparently answered here:

How do you explicitly set a new property on `window` in TypeScript?

I can't mark my own post as a duplicate so I will just leave this here and close the thread, maybe the next poor sod to google search and not click that link will stumble upon this.

Apparently properties of an object (e.g window) can be queried with the array selection operator (o.o?) so you can just do:

window["UndeclaredExportedProperty"].aFunction()
发布评论

评论列表(0)

  1. 暂无评论