如何在TypeScript中处理内置对象的专有/自定义属性的警告 [英] How to handle warnings for proprietary/custom properties of built-in objects in TypeScript

查看:1103
本文介绍了如何在TypeScript中处理内置对象的专有/自定义属性的警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的角色依赖于专有财产 navigator.id 。由于此属性不是标准属性,因此TypeScript编译器会生成以下警告:

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:

$ 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'

但.js文件已成功生成并在FF15浏览器上运行,没有任何警告/错误消息。

我还包含一个polyfill for navigator.id ,按照文档的说明,所以 navigator.id 肯定会在每个浏览器中提供。

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.

有人可以建议我如何处理此警告吗?

Could someone suggest me how to deal with this warning?

index.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

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);
});


推荐答案

1)
你可以重新解释导航道具。

1) You can reinterpret navigator prop.

(<any>navigator).id.request();

2)
你可以自己声明id prop

2) You can declare id prop youself

mycompany.lib.d.ts

mycompany.lib.d.ts

interface Navigator {
  id: any
}

app.ts

navigator.id.request();

观看此视频 http://channel9.msdn.com/posts/Anders-Hejlsberg-Introducing-TypeScript/ Anders告诉jQuery.UI向jQuery添加新方法(参见46分钟)

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)

这篇关于如何在TypeScript中处理内置对象的专有/自定义属性的警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆