我正在使用的角色依赖于 专有财产 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'

但 .js 文件已成功生成并在 FF15 浏览器上运行,没有任何警告/错误消息。
我还提供了一个polyfill navigator.id, ,按照文档的指示,所以 navigator.id 肯定会在每个浏览器中可用。

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

索引.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 -->

主页.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)您可以重新诠释导航器道具。

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

2)您可以声明自己的身份证

mycompany.lib.d.ts

interface Navigator {
  id: any
}

应用程序.ts

navigator.id.request();

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

其他提示

添加检查,例如 if(navigator.id != null && typeof navigator.id != 'undefined') 在 stmt 之前,其中 navigator.id 被引用

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top