ts 里类似 golang 这种写法怎么实现?

61 天前
 imherer

假如我现在有一个status,它的值是数字,但是需要有一个中文的说明,于是在 golang 里可以这样写

go:
type Status int

const Success Status = 1
const Failed Status = 2

func (s Success) ToString() string{
	return "成功"
}
func (s Failed) ToString() string{
	return "失败"
}

在使用的时候,如果我想要字符串的说明直接 Success.ToString()就行了

ts 里有类似的实现吗?

我现在是这样写的

ts:
export enum Status {
  Success = { id: 1, value: "成功" },
  Failed = { id: 2, value: "失败" },
}

使用:Status.Success.value

2629 次点击
所在节点    程序员
22 条回复
yukinomiu
61 天前
你确定 golang 可以这么写?
sunny352787
61 天前
别瞎说啊,golang 不是这么写的
imherer
61 天前
@yukinomiu
@sunny352787
不好意思,确实是写错了,应该是把 Success 和 Failed 分别定义 type
Vegetable
61 天前
你这 golang 的代码根本不对,实际上写法会比这个丑很多,因为你没办法为值编写方法,你只能为类型编写方法。
写出来大概就是:



type Status int

const Success Status = 1
const Failed Status = 2

func (s Status) ToString() string {
if s == Failed {
return "失败"
}
if s == Success {
return "成功"
}
return "成功"
}

这样值和类型方法耦合,我觉得相当难看,ts 想做成这样单独定义一个 explain(value)也没啥区别吧
pangdundun996
61 天前
```go
type Status int
const Success Status = 1
const Failed Status = 2
func (s Status) ToString() string {
if s == Success {
return "success"
}
return "failed"
}
```
方法只能绑到 type 上吧
LuckyLauncher
61 天前
你是不是混淆了类型和值的概念
enchilada2020
61 天前
enum Status {
Success = 1,
Failed
}

console.log(Status.Success)
console.log(Status[Status.Success])

建议翻翻文档 Reverse Mappings 那节 清清楚楚写着呢
kriszu
61 天前
你要在 ts 里实现这种效果,没必要用枚举啊,枚举的值只能是 string,number,boolean ,你可以定义一个对象或者类来实现
horizon
61 天前
自定义 enum class ,然后自己实现 toString 方法
另外,ts 里不建议用自带的 enum
https://mkosir.github.io/typescript-style-guide/#enums--const-assertion
sunny352787
61 天前
@Vegetable 丑点无所谓,反正都是 stringer 生成
DiamondYuan
61 天前
class Status {
construtor ( private value:number ,private label:string )


toString (){
return this.label


valueOf (){
return this.value






const success = new Status ( 1 ,“成功”)
imherer
61 天前
@enchilada2020 原来可以这样,不过我想要中文说明的话 key 定义成中文感觉有点怪怪的
imherer
61 天前
Morriaty
61 天前
你需要比较枚举值吗?不需要的话,直接用 string 定义枚举啊
imherer
61 天前
@Morriaty 需要比较的。 主要是 string 是中文,枚举定义成中文感觉有点怪怪的
wpzz
61 天前
不能这么写,状态 KeyVal ,和状态定义需要解耦。
FanGanXS
61 天前
```go
var StatusMap = map[Status]string{
Success: "成功",
Failed: "失败",
}

type Status int

const (
Success Status = iota
Failed
)

func (s Status) ToString() string {
return StatusMap[s]
}

golang 的写法应该是这样,用 map 来映射 status 和字符串就好了。
这样写的时候只需要关注 map 而不需要关注 ToString 的实现。
lovedebug
61 天前
GPT 给的写法

enum Status {
Success = 1,
Failed = 2,
}

const StatusMessages = {
[Status.Success]: "成功",
[Status.Failed]: "失败",
};

function getStatusMessage(status: Status): string {
return StatusMessages[status];
}

// 使用示例
console.log(getStatusMessage(Status.Success)); // 输出: 成功
console.log(getStatusMessage(Status.Failed)); // 输出: 失败
lisongeee
61 天前
export const Success = { id: 1, value: '成功' } as const;
export const Failed = { id: 2, value: '失败' } as const;
export type Status = typeof Success | typeof Failed;
supuwoerc
61 天前
export enum RoomTypes {
OR = "手术室",
SVG = "抢救室",
}

export const RoomTypeOptions = [
{
key: RoomTypes[RoomTypes.OR],
value: 1,
label: RoomTypes.OR,
},
{
key: RoomTypes[RoomTypes.SVG],
value: 2,
label: RoomTypes.SVG,
},
];

// --------------------------------

export const ROOM_OPTIONS = {
OR: {
key: "OR",
value: 1,
label: "手术室",
},
SVG: {
key: "SVG",
value: 2,
label: "急救室",
},
} as const;

export type RoomTypeKey = keyof typeof ROOM_OPTIONS;

export type RoomType = (typeof ROOM_OPTIONS)[RoomTypeKey];

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1057893

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX