Javascript 如何处理 TypeScript 中内置对象的专有/自定义属性的警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12703266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to handle warnings for proprietary/custom properties of built-in objects in TypeScript
提问by GiVeR
I am using Personas which relies on the proprietary property navigator.id
. Since this property is not standard, the TypeScript compiler generates the following warning:
我正在使用依赖于专有财产的navigator.id
角色。由于此属性不是标准属性,因此 TypeScript 编译器会生成以下警告:
$ tsc home.ts --out my_ts_generated_code.js
/Users/..../home.ts(27,18): The property 'id' does not exist on value of type 'Navigator'
But the .js file is successfully generated and runs on the FF15 browser without any warning/error message.
I also include a polyfill for navigator.id
, as instructed by the documentation, so navigator.id
will definitely by available in every browser.
但是 .js 文件成功生成并在 FF15 浏览器上运行,没有任何警告/错误消息。
我还包括一个 polyfill for navigator.id
,按照文档的说明,所以navigator.id
肯定会在每个浏览器中可用。
Could someone suggest me how to deal with this warning?
有人可以建议我如何处理这个警告吗?
index.html
索引.html
<!-- some HTML omit above -->
<script src="https://login.persona.org/include.js"></script>
<script src="my_ts_generated_code.js"></script>
<button class="btn" id="signin">Sign in</button>
<button class="btn" id="signout">Sign out</button>
<!-- some HTML omit below -->
home.ts
家.ts
declare var $;
class Student {
fullname : string;
constructor(public firstname, public middleinitial, public lastname) {
this.fullname = firstname + " " + middleinitial + " " + lastname;
}
}
interface Person {
firstname: string;
lastname: string;
}
function greeter(person : Person) {
return "Hello, " + person.firstname + " " + person.lastname;
}
var user = new Student("Jane", "M.", "User");
$(function() {
$('#signin').on('click', function(e) {
e.preventDefault();
navigator.id.request();
});
$('#signout').on('click', function(e) {
e.preventDefault();
navigator.id.logout();
});
//document.body.innerHTML = greeter(user);
});
回答by Hyman128
1) You can reinterpret navigator prop.
1)您可以重新解释导航器道具。
(<any>navigator).id.request();
2) You can declare id prop youself
2)你可以自己声明id道具
mycompany.lib.d.ts
mycompany.lib.d.ts
interface Navigator {
id: any
}
app.ts
应用程序
navigator.id.request();
see this video http://channel9.msdn.com/posts/Anders-Hejlsberg-Introducing-TypeScript/There Anders tell as jQuery.UI add new methods to jQuery (see 46 min)
观看此视频http://channel9.msdn.com/posts/Anders-Hejlsberg-Introducing-TypeScript/Anders 讲述了 jQuery.UI 向 jQuery 添加新方法(见 46 分钟)
回答by rt2800
Add checks like if(navigator.id != null && typeof navigator.id != 'undefined')
before stmt where navigator.id is referred
if(navigator.id != null && typeof navigator.id != 'undefined')
在引用 navigator.id 的 stmt 之前添加检查