@saltify/milky-types 1.2.0 → 1.2.1-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,57 @@
1
- # Milky Types
1
+ # @saltify/milky-types
2
2
 
3
- 这是 Milky 协议的 TypeScript 类型定义包,使用 Zod 进行运行时类型验证。
3
+ 这是 Milky 协议的 TypeScript 类型定义包,使用 [Zod](https://zod.dev/) 进行运行时类型验证。
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ npm install @saltify/milky-types
9
+ ```
10
+
11
+ ## 使用方法
12
+
13
+ ### 进行运行时类型验证
14
+
15
+ ```typescript
16
+ import { FriendEntity } from '@saltify/milky-types';
17
+
18
+ const friend1 = FriendEntity.parse({
19
+ user_id: 123456789,
20
+ nickname: 'Alice',
21
+ sex: 'female',
22
+ qid: 'Saltify',
23
+ remark: 'Best friend',
24
+ category: {
25
+ id: 1,
26
+ name: 'Close Friends',
27
+ },
28
+ }); // 成功解析,friend1 的类型为 FriendEntity
29
+
30
+ const friend2 = FriendEntity.parse({
31
+ user_id: 'not a number',
32
+ nickname: 'Bob',
33
+ }); // 解析失败,抛出 ZodError 错误
34
+ ```
35
+
36
+ ### 获取 TypeScript 类型定义
37
+
38
+ ```typescript
39
+ import { FriendEntity } from '@saltify/milky-types';
40
+
41
+ const friend1: FriendEntity = {
42
+ user_id: 123456789,
43
+ nickname: 'Alice',
44
+ sex: 'female',
45
+ qid: 'Saltify',
46
+ remark: 'Best friend',
47
+ category: {
48
+ id: 1,
49
+ name: 'Close Friends',
50
+ },
51
+ }; // friend1 的类型为 FriendEntity
52
+
53
+ const friend2: FriendEntity = {
54
+ user_id: 'not a number',
55
+ nickname: 'Bob',
56
+ }; // TypeScript 编译错误,user_id 应为 number 类型,并且缺少 sex、qid、remark 和 category 字段
57
+ ```