【问题标题】:TypeScript - Initialise property from another JavaScript fileTypeScript - 从另一个 JavaScript 文件初始化属性
【发布时间】:2012-10-05 16:54:41
【问题描述】:

我同时使用 TypeScript 和 Firebase。

我的班级有fireybase: FirebaseInterface; 属性。

当我使用this.fireybase = new Firebase('URL/');初始化它时

我收到关于 Firebase 对象的编译时错误。

The name 'Firebase' does not exist in the current scope

interface FirebaseInterface {
    set(s:string): any;
}

我已尝试将 Firebase 接口添加到库中,但它仍然不喜欢它。

在 TypeScript 中执行此操作的最佳方法是什么?

【问题讨论】:

  • 我对 Firebase 一无所知,但如果我错了,请纠正我,我在您的代码中看到 this.fireybase。你的“fireybase”中应该有一个y吗?只是检查这是否不是PEBKAC 错误。
  • Max:多一点代码对发现问题非常有帮助。

标签: typescript firebase


【解决方案1】:

typescript 编译器告诉你找不到类型Firebase,所以你需要引入它。在您的情况下,如果 firebase 库的提供者想要支持 Typescript 中的开发,他们将需要提供一个声明文件,该文件指定类型 Firebase (和依赖类型)。在您的情况下,一个简单的 Firebase.d.ts 文件可能如下所示:

Firebase.d.ts

declare class Firebase {    
    constructor(u:string);    
    set(s:string): any;
}

然后你需要将声明文件带入引用声明的范围,如下所示:

MyApp.ts

///<reference path='firebase.d.ts'/>
class MyApp {  
    fireybase: Firebase;  
    constructor () => {
        this.fireybase = new Firebase('URL/');
    }
}

您可以在language specification 中阅读有关声明文件和引用路径的更多信息。

【讨论】:

    【解决方案2】:

    我认为您需要引用包含 Firebase 静态定义的模块。也许快速解决方法是在文件顶部使用以下语句:

    declare var Firebase: any;

    【讨论】:

      猜你喜欢
      • 2021-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-17
      • 2020-12-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多