类中的枚举(TypeScript 定义文件)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29844959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Enum inside class (TypeScript definition file)
提问by Lewis Peel
I've searched around but can't seem to find an answer for this, hopefully you can help. How can I add an enum to Image? This is what I would like ideally but I get an error.
我已经四处搜索,但似乎无法找到答案,希望您能提供帮助。如何向 Image 添加枚举?这是我理想中想要的,但出现错误。
declare module 'Lib' {
export module Graphics {
export class Image {
enum State {}
static STATE_IDLE: State;
static STATE_LOADING: State;
static STATE_READY: State;
static STATE_ERROR: State;
constructor();
}
}
}
If I move State into the Graphics module it works but now State belong to Graphics...which is incorrect, it needs to be part of Image.
如果我将状态移动到图形模块中,它可以工作,但现在状态属于图形......这是不正确的,它需要成为图像的一部分。
Any ideas? Thanks
有任何想法吗?谢谢
采纳答案by Lewis Peel
I think I may have found a solution...whether it's valid TypeScript I don't know but it works and doesn't cause any compile errors. It's a combination of the above answers.
我想我可能已经找到了一个解决方案......我不知道它是否是有效的 TypeScript,但它可以工作并且不会导致任何编译错误。这是上述答案的组合。
declare module 'Lib' {
module Graphics {
module Image {
enum State { }
var STATE_IDLE: State;
var STATE_LOADING: State;
var STATE_READY: State;
var STATE_ERROR: State;
}
class Image {
constructor();
}
}
}
Can anyone spot any potential issues with this that I haven't noticed?
任何人都可以发现我没有注意到的任何潜在问题吗?
回答by NSjonas
I think the following is an improvement on KoenT's solution:
我认为以下是对 KoenT 解决方案的改进:
export class Image
{
constructor ()
{
this.state = Image.State.Idle;
}
state: Image.State;
}
export namespace Image
{
export enum State
{
Idle,
Loading,
Ready,
Error
}
}
The advantage being that you can leverage named imports:
优点是您可以利用命名导入:
import {Image} from './image';
let img = new Image()
img.state = Image.State.Error
回答by KoenT
I also bumped into this problem recently. This is what I am currently using as a solution:
我最近也碰到了这个问题。这是我目前使用的解决方案:
// File: Image.ts
class Image
{
constructor()
{
this.state = Image.State.Idle;
}
state: Image.State;
}
module Image
{
export enum State
{
Idle,
Loading,
Ready,
Error
}
}
export = Image;
Then in the place where I'm using the class and its enum:
然后在我使用类及其枚举的地方:
import Image = require("Image");
let state = Image.State.Idle;
let image = new Image();
state = image.state;
This seems to work fine (even though I don't consider it as the expected way to do this kind of thing).
这似乎工作正常(即使我不认为它是做这种事情的预期方式)。
Hopefully there will be a way in TypeScript to do it this way:
希望在 TypeScript 中有一种方法可以这样做:
class Image
{
enum State
{
Idle,
Loading,
Ready,
Error
}
constructor()
{
this.state = State.Idle;
}
state: State;
}
export = Image;
回答by Ma?vydas Tadaravi?ius
Here's my solution.
这是我的解决方案。
program.ts:
程序.ts:
enum Status {
Deleting,
Editing,
Existing,
New
}
export class Program {
static readonly Status = Status;
readonly Status = Program.Status;
title: string;
status: Status;
constructor(init?: Partial<Program>) {
Object.assign(this, init);
}
}
Usage:
用法:
let program = new Program({ title: `some title` });
program.status = Program.Status.New;
or
或者
program.status = program.Status.New;
Added benefit for Angular 2+ users:this can be used in templates
Angular 2+ 用户的额外好处:这可以在模板中使用
<div *ngIf="program.status === program.Status.New">
Only display if status of the program is New
</div>
回答by Alex
I think that this stuff with module augmentation is a very hacky and non-intuitive way* of doing things, so consider this:
我认为这种带有模块增强的东西是一种非常笨拙且不直观的做事方式*,因此请考虑:
export module Graphics
{
enum State
{
STATE_IDLE,
STATE_LOADING,
STATE_READY,
STATE_ERROR
}
export class Image
{
constructor() { }
public static readonly State = State;
}
}
//...
let imgState = Graphics.Image.State.STATE_ERROR;
That is, just declare the enum in the scope of the class that you want to add it to without exporting it, then expose it through a member of the class.
也就是说,只需在要添加它的类的范围内声明枚举而不导出它,然后通过类的成员公开它。
* Which in regards of structuring and organization of code is BAD, even if it technically works.
* 这在代码的结构和组织方面是糟糕的,即使它在技术上可行。
Update
更新
declare module Lib
{
enum State
{
STATE_IDLE,
STATE_LOADING,
STATE_READY,
STATE_ERROR
}
class ImageClass
{
constructor();
public Prop: any;
}
export interface Graphics
{
Image: typeof State & ImageClass & (new () => typeof State & ImageClass);
}
}
declare var Graphics: Lib.Graphics;
Then you get typing like:
然后你会打字:
var someEnum = Graphics.Image.STATE_ERROR;
var image = new Graphics.Image();
var anotherEnum = image.STATE_IDLE;
回答by Fenton
I'm not sure what you intend to do, but I would have expected that you would want an enum
to represent the possible state values, and then a state
member on the image to indicate the current state of the image.
我不确定您打算做什么,但我希望您希望使用 anenum
来表示可能的状态值,然后使用state
图像上的成员来指示图像的当前状态。
declare module 'Lib' {
export module Graphics {
enum State {
STATE_IDLE,
STATE_LOADING,
STATE_READY,
STATE_ERROR
}
export class Image {
public state: State;
constructor();
}
}
}
It sounds like you want to declare a class that has enum-like members, rather than declare an enum within a class. i.e:
听起来您想声明一个具有类枚举成员的类,而不是在类中声明一个枚举。IE:
declare module 'Lib' {
export module Graphics {
export class Image {
static STATE_IDLE: number;
static STATE_LOADING: number;
static STATE_READY: number;
static STATE_ERROR: number;
constructor();
}
}
}
回答by David Sherret
You could create a module and class with the same name. It might also help to rename your enum so that you don't have to say State
twice:
您可以创建一个具有相同名称的模块和类。重命名您的枚举也可能有帮助,这样您就不必重复State
两次:
declare module 'Lib' {
export module Graphics {
export class Image {
constructor();
}
export module Image {
export enum State {
Idle,
Loading,
Ready,
Error
}
}
}
}