简体   繁体   English

TypeScript中模块上的getter / setter

[英]getter/setter on a module in TypeScript

I am using AMD modules (compiler flag "--module amd") in my TypeScript project. 我在我的TypeScript项目中使用AMD模块(编译器标志“--module amd”)。 While I can easily use getters/setters on my classes I would like to do the same on my modules but 虽然我可以在我的课程上轻松使用getter / setter,但我想在我的模块上做同样的事情

export get abc() : string {
    return "abc";
}

returns 回报

error TS1008: Unexpected token; 错误TS1008:意外的令牌; 'module, class, interface, enum, import or statement' expected. '模块,类,接口,枚举,导入或声明'预期。

and

export function get abc() : string {
    return "abc";
}

returns 回报

error TS1005: '(' expected. 错误TS1005:'('预期。

What am I doing wrong? 我究竟做错了什么?

You can only add getters and setters to a class at the moment. 您现在只能将getter和setter添加到类中。

The code transformation TypeScript uses on getters and setters adds the property to the prototype of the object, which makes more sense for classes than for modules. TypeScript在getter和setter上使用的代码转换将属性添加到对象的原型中,这对于类而言比对模块更有意义。

This is possible, using the special export = ... syntax as follows: 这是可能的,使用特殊的export = ...语法如下:

class MyModule {
    get abc() {
        return "abc";
    }
}

var myModule = new MyModule();
export = myModule;

This makes an instance of the class MyModule act as the API of the module. 这使得MyModule类的实例充当模块的API。 You don't have to put any data in the class - just move your functions into it and otherwise leave them unchanged. 您不必在类中放置任何数据 - 只需将您的函数移入其中,否则保持不变。 The downside is that if function a calls function b it will have to say this.b() or myModule.b() (the latter is closer to a normal module export). 缺点是如果函数a调用函数b ,则必须说this.b()myModule.b() (后者更接近正常的模块导出)。

Also you have to declare a named variable first. 您还必须先声明一个命名变量。 You can't just say: 你不能只说:

export = new MyModule(); // This doesn't work

Just for reference of future visitors ... 仅供未来访客参考......

Similar to what @Daniel Earwicker was saying and explaining what @billc.cn was saying, you can also conflate a class with a namespace and then just define the getter/setter as a static method: 类似于@Daniel Earwicker所说的并解释@ billc.cn所说的内容,您也可以将类与命名空间混淆,然后将getter / setter定义为静态方法:

export class X {
   static get abc():string {
      return "abc";
   }
}

export namespace X {
  // ... other code
}

But this means that you will have a namespace within your library (module) and unless you want to change the way that you address attributes of your library, you will have to do the hack export = X; 但这意味着你的库(模块)中将有一个命名空间,除非你想改变你的库属性的方式,你将不得不做hack export = X; that @Daniel Earwicker mentioned. @Daniel Earwicker提到。

https://www.typescriptlang.org/docs/handbook/declaration-merging.html https://www.typescriptlang.org/docs/handbook/declaration-merging.html

You can export your getter directly, via a class, like this: 您可以通过类直接导出您的getter,如下所示:

// helper class to be removed when/if module-level getters are available
class _someHelper {
    static get myStaticString(): string {
        return "hi, world";
    }
}

export const myStaticString = _someHelper.myStaticString;

Then import like: 然后导入如下:

import { myStaticString } from "my-module";

Not the prettiest, but it does work. 不是最漂亮的,但确实有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM