Meteor & TypeScript - Error: The property 'confirm' does not exist on value of type '{}'

Meteor & TypeScript - Error: The property 'confirm' does not exist on value of type '{}'

我正在使用 Meteor 和 TypeScript 开发 Web 应用程序,也使用 Nitrous.io 环境。

我正在尝试实施用户帐户系统。我正在调整我在旧项目中的 JavaScript 代码(它有效),但我仍然遇到错误。这是来自 login.ts 文件:

// retrieve the input field values
        var email = template.$('[name=email]').val();
        var password = template.$('[name=password]').val();

        var errors = {};

        if (! email) {
            errors.email = 'Please enter your email address';     // ERROR HERE
        }

        if (! password) {
            errors.password = 'Please enter your password';       // ERROR HERE
        } 

我收到的错误消息是:

/client/login.ts(41,20): error TS2094: The property 'email' does not exist on value of type '{}'.                                                                                                                                           

/client/login.ts(45,20): error TS2094: The property 'password' does not exist on value of type '{}'.

有什么想法吗?谢谢。 :)

将错误对象更改为此。

var errors = {
   email:"",
   password:""
};

你也可以抛出错误对象(每个浏览器都支持这两个属性)

  1. name - 错误的名称
  2. 消息 - 错误描述

或自定义错误。

if (! email) {
            throw new Error('Please enter your email address')     // ERROR HERE
        }

        if (! password) {
            throw new Error('Please enter your password')       // ERROR HERE
        } 

如果您对抛出错误感兴趣,请勾选 this article