Skip to content

Commit 2060cc8

Browse files
committed
add string-based enum example
1 parent 5d554dc commit 2060cc8

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

docs/contributing/code-style/enums.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,21 @@ type CipherContent =
5858
| { type: typeof CipherType.SecureNote, note: EncString, ... }
5959
```
6060
61+
The above pattern also works with string-typed enum members:
62+
63+
```ts
64+
// freeze to prevent member injection
65+
export const CredentialType = Object.freeze({
66+
Password: "password",
67+
Username: "username",
68+
Email: "email",
69+
SshKey: "ssh-key",
70+
} as const);
71+
72+
// derive the enum-like type from the raw data
73+
export type CredentialType = CredentialType[keyof typeof CredentialType];
74+
```
75+
6176
:::warning
6277

6378
Unlike an enum, TypeScript lifts the type of the members of `const CipherType` to `number`. Code

0 commit comments

Comments
 (0)