作为我上一个有关bowser
专门使用DefinitelyTyped定义文件的问题的后续,我import
按照该问题中答案的指示实施了该语句,并进一步介绍了该语句。但是,现在TypeScript编译器抱怨在所有Bowser导入废话之前完美编译的代码。
假设我有MyBowserClass.ts:
import bowser = require('bowser');
namespace MyNamespace {
export class MyBowserClass {
constructor() {
var isIos = (typeof bowser.ios === 'undefined') ? false : bowser.ios;
alert(isIos);
var myInstance = new MyNamespaceTwo.MyOtherClass(); // typescript compiler complains: Property 'MyOtherClass' does not exist on type 'typeof MyNamespaceTwo'.
}
}
}
然后我有MyOtherClass.ts:
namespace MyNamespaceTwo {
export class MyOtherClass {
constructor() {
alert('otherclass ctor');
}
}
}
编译器在这里给我一个错误:
var myInstance = new MyNamespaceTwo.MyOtherClass();
说 Property 'MyOtherClass' does not exist on type 'typeof MyNamespaceTwo'.
所以我想也许这意味着我也需要导入MyOtherClass
吗?
我通过更新两个文件来使其工作:
import bowser = require('bowser');
import otherClass = require('MyOtherClass'); // NEW IMPORT
namespace MyNamespace {
export class MyBowserClass {
constructor() {
var isIos = (typeof bowser.ios === 'undefined') ? false : bowser.ios;
alert(isIos);
var myInstance = new otherClass.MyNamespaceTwo.MyOtherClass(); // changed this to prefix with 'otherClass'
}
}
}
export namespace MyNamespaceTwo { // made this EXPORT
export class MyOtherClass {
constructor() {
alert('otherclass ctor');
}
}
}
这似乎完全混乱/疯狂。我在这里想念什么?为什么bowser
定义文件无论如何都应该是一个模块(当它由全局/本质上是静态的方法名称组成时??)任何指导/帮助将不胜感激。
似乎您要将文件从全局声明文件更改为模块声明文件。
一个全局声明文件使得其在整个项目可访问声明的类型,而无需输入任何东西。全局声明文件永远不能从另一个文件导入。它们也永远不会导出,因为其中声明的类型随处可见。
例如。使用redux的项目可以声明一个SpecialAction
可以在项目中任何地方使用的项目:
// index.d.ts
interface SpecialAction {
type: string
isSpecial: boolean
}
// src/app.ts
let theAction: SpecialAction = {
type: 'SPECIAL',
isSpecial: true
}
一个模块声明文件出口特定类型的模块,所以出口可以在其他地方在项目中导入。在声明文件中导入或导出后,它将立即成为模块声明文件。
// index.d.ts
import { Action } from 'redux'
export interface SpecialAction extends Action {
isSpecial: boolean
}
// src/app.ts
import { SpecialAction } from '../index'
let theAction: SpecialAction = {
type: 'SPECIAL',
isSpecial: true
}
我希望这有帮助? ¯\_(ツ)_/¯
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句