Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit 56bab7c

Browse files
authored
refactor(types): enhance type system (#1299)
* refactor(type): use Record for json constant * refactor(type): use Record for json constant * chore(eslint): clean up import type error * chore(type): extract third lib to spec
1 parent 8a37ba5 commit 56bab7c

File tree

14 files changed

+113
-78
lines changed

14 files changed

+113
-78
lines changed

.eslintrc.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ module.exports = {
88
'@groupher/eslint-config-web',
99
'eslint:recommended',
1010
'plugin:@typescript-eslint/recommended',
11+
// see: https://stackoverflow.com/a/56696478/4050784
12+
'plugin:import/typescript',
1113
],
1214
// extends: ['@groupher/eslint-config-web'],
1315
settings: {

package-lock.json

Lines changed: 26 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@
161161
"shelljs": "0.8.4",
162162
"standard-version": "^8.0.1",
163163
"start-server-and-test": "1.12.1",
164+
"type-fest": "^2.12.2",
164165
"typescript": "^4.2.3"
165166
}
166167
}

src/spec/article.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ export type TUpvoteLayout =
210210
| 'comment'
211211
| 'article'
212212
| 'post-list'
213+
| 'blog-list'
213214
| 'works-article'
214215
| 'works-card'
215216
| 'guide-list'

src/spec/enhance.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// see: https://github.com/sindresorhus/type-fest#template-literal-types
2+
export type { ScreamingSnakeCase as SnakeUpperCase } from 'type-fest'
3+
export type holder = 1

src/spec/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import type { TRootStore as RootStoreType } from '@/stores/RootStore'
2+
23
import type { TArticle } from './article'
34
import type { TCommunity } from './community'
45

6+
export type { SnakeUpperCase } from './enhance'
7+
58
export type { TMetric } from './metric'
69
export type {
710
TSIZE,

src/spec/utils.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,11 @@ export type TTabItem = {
9696

9797
export type TResState = 'LOADING' | 'DONE' | 'EMPTY'
9898

99-
export type TPaymentUsage = 'SENIOR' | 'GirlsCodeTooPlan' | 'DONATE' | 'SPONSOR'
99+
export type TPaymentUsage =
100+
| 'SENIOR'
101+
| 'GIRLS_CODE_TOO_PLAN'
102+
| 'DONATE'
103+
| 'SPONSOR'
100104
export type TPaymentMethod = 'ALIPAY' | 'WECHAT'
101105

102106
export type TFlexRule =
@@ -183,7 +187,7 @@ export type TModelineType =
183187
| 'search'
184188
| 'more'
185189

186-
export type TGtdType = 'BUG' | 'FEATURE' | null
187-
export type TGtdState = 'TODO' | 'WIP' | 'DONE' | null
190+
export type TGtdType = 'BUG' | 'FEATURE' | 'DEFAULT'
191+
export type TGtdState = 'TODO' | 'WIP' | 'DONE' | 'DEFAULT'
188192

189193
export type TTagMode = 'default' | 'label'

utils/constant/GTD.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import type { TGtdType, TGtdState } from '@/spec'
22

33
export const GTD_TYPE = {
4-
FEATURE: 'FEATURE' as TGtdType,
5-
BUG: 'BUG' as TGtdType,
6-
DEFAULT: null as TGtdType,
7-
}
4+
FEATURE: 'FEATURE',
5+
BUG: 'BUG',
6+
DEFAULT: 'DEFAULT',
7+
} as Record<Uppercase<TGtdType>, Uppercase<TGtdType>>
88

99
export const GTD_STATE = {
10-
TODO: 'TODO' as TGtdState,
11-
WIP: 'WIP' as TGtdState,
12-
DONE: 'DONE' as TGtdState,
13-
DEFAULT: null as TGtdState,
14-
}
10+
TODO: 'TODO',
11+
WIP: 'WIP',
12+
DONE: 'DONE',
13+
DEFAULT: 'DEFAULT',
14+
} as Record<Uppercase<TGtdState>, Uppercase<TGtdState>>

utils/constant/emotion.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import type { TEmotionType } from '@/spec'
22

33
const EMOTION = {
4-
DOWNVOTE: 'downvote' as TEmotionType,
5-
BEER: 'beer' as TEmotionType,
6-
HEART: 'heart' as TEmotionType,
7-
CONFUSED: 'confused' as TEmotionType,
8-
POPCORN: 'popcorn' as TEmotionType,
9-
PILL: 'pill' as TEmotionType,
10-
}
4+
DOWNVOTE: 'downvote',
5+
BEER: 'beer',
6+
HEART: 'heart',
7+
CONFUSED: 'confused',
8+
POPCORN: 'popcorn',
9+
PILL: 'pill',
10+
} as Record<Uppercase<TEmotionType>, TEmotionType>
1111

1212
export default EMOTION

utils/constant/layout.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import type { TUpvoteLayout } from '@/spec'
1+
import type { TUpvoteLayout, SnakeUpperCase } from '@/spec'
22

33
export const UPVOTE_LAYOUT = {
4-
DEFAULT: 'default' as TUpvoteLayout,
5-
COMMENT: 'comment' as TUpvoteLayout,
6-
ARTICLE: 'article' as TUpvoteLayout,
7-
POST_LIST: 'post-list' as TUpvoteLayout,
8-
BLOG_LIST: 'blog-list' as TUpvoteLayout,
9-
WORKS_ARTICLE: 'works-article' as TUpvoteLayout,
10-
WORKS_CARD: 'works-card' as TUpvoteLayout,
11-
GUIDE_LIST: 'guide-list' as TUpvoteLayout,
12-
}
4+
DEFAULT: 'default',
5+
COMMENT: 'comment',
6+
ARTICLE: 'article',
7+
POST_LIST: 'post-list',
8+
BLOG_LIST: 'blog-list',
9+
WORKS_ARTICLE: 'works-article',
10+
WORKS_CARD: 'works-card',
11+
GUIDE_LIST: 'guide-list',
12+
} as Record<SnakeUpperCase<TUpvoteLayout>, TUpvoteLayout>
1313

1414
export const holder = 1

utils/constant/metric.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,32 @@ import type { TMetric } from '@/spec'
22
// NOTE: the value is mapping to @/utils/media's key
33
// so do not change to lowercase etc...
44
const METRIC = {
5-
COMMUNITY: 'COMMUNITY' as TMetric,
6-
USER: 'USER' as TMetric,
5+
COMMUNITY: 'COMMUNITY',
6+
USER: 'USER',
77
// article
8-
ARTICLE: 'ARTICLE' as TMetric,
9-
BLOG_ARTICLE: 'BLOG_ARTICLE' as TMetric,
10-
WORKS_ARTICLE: 'WORKS_ARTICLE' as TMetric,
8+
ARTICLE: 'ARTICLE',
9+
BLOG_ARTICLE: 'BLOG_ARTICLE',
10+
WORKS_ARTICLE: 'WORKS_ARTICLE',
1111

1212
// 版块
13-
WORKS: 'WORKS' as TMetric,
14-
TRENDING: 'TRENDING' as TMetric,
15-
COOL_GUIDE: 'COOL_GUIDE' as TMetric,
16-
HAVE_A_DRINK: 'HAVE_A_DRINK' as TMetric,
17-
RECIPES: 'RECIPES' as TMetric,
18-
EXPLORE: 'EXPLORE' as TMetric,
19-
SPONSOR: 'SPONSOR' as TMetric,
20-
FRIENDS: 'FRIENDS' as TMetric,
21-
SUPPORT_US: 'SUPPORT_US' as TMetric,
22-
SUBSCRIBE: 'SUBSCRIBE' as TMetric,
23-
MEETUPS: 'MEETUPS' as TMetric,
24-
MEMBERSHIP: 'MEMBERSHIP' as TMetric,
25-
HELP_CENTER: 'HELP_CENTER' as TMetric,
13+
WORKS: 'WORKS',
14+
TRENDING: 'TRENDING',
15+
COOL_GUIDE: 'COOL_GUIDE',
16+
HAVE_A_DRINK: 'HAVE_A_DRINK',
17+
RECIPES: 'RECIPES',
18+
EXPLORE: 'EXPLORE',
19+
SPONSOR: 'SPONSOR',
20+
FRIENDS: 'FRIENDS',
21+
SUPPORT_US: 'SUPPORT_US',
22+
SUBSCRIBE: 'SUBSCRIBE',
23+
MEETUPS: 'MEETUPS',
24+
MEMBERSHIP: 'MEMBERSHIP',
25+
HELP_CENTER: 'HELP_CENTER',
2626

2727
// eidtors
28-
WORKS_EDITOR: 'WORKS_EDITOR' as TMetric,
29-
COMMUNITY_EDITOR: 'COMMUNITY_EDITOR' as TMetric,
30-
ARTICLE_EDITOR: 'ARTICLE_EDITOR' as TMetric,
31-
}
28+
WORKS_EDITOR: 'WORKS_EDITOR',
29+
COMMUNITY_EDITOR: 'COMMUNITY_EDITOR',
30+
ARTICLE_EDITOR: 'ARTICLE_EDITOR',
31+
} as Record<Uppercase<TMetric>, Uppercase<TMetric>>
3232

3333
export default METRIC

utils/constant/payment.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import type { TPaymentUsage, TPaymentMethod } from '@/spec'
22

33
export const PAYMENT_USAGE = {
4-
SENIOR: 'SENIOR' as TPaymentUsage,
5-
GIRLS_CODE_TOO_PLAN: 'GirlsCodeTooPlan' as TPaymentUsage,
6-
DONATE: 'DONATE' as TPaymentUsage,
7-
SPONSOR: 'SPONSOR' as TPaymentUsage,
8-
}
4+
SENIOR: 'SENIOR',
5+
GIRLS_CODE_TOO_PLAN: 'GIRLS_CODE_TOO_PLAN',
6+
DONATE: 'DONATE',
7+
SPONSOR: 'SPONSOR',
8+
} as Record<Uppercase<TPaymentUsage>, Uppercase<TPaymentUsage>>
99

1010
export const PAYMENT_METHOD = {
11-
ALIPAY: 'ALIPAY' as TPaymentMethod,
12-
WECHAT: 'WECHAT' as TPaymentMethod,
13-
}
11+
ALIPAY: 'ALIPAY',
12+
WECHAT: 'WECHAT',
13+
} as Record<Uppercase<TPaymentMethod>, Uppercase<TPaymentMethod>>

utils/constant/tab.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import type { TWorksTab, TBlogTab } from '@/spec'
22

33
export const WORKS_TAB = {
4-
STORY: 'story' as TWorksTab,
5-
BASIC: 'basic' as TWorksTab,
6-
TECHSTACKS: 'techstacks' as TWorksTab,
7-
COMMUNITY: 'community' as TWorksTab,
8-
MILESTONE: 'milestone' as TWorksTab,
9-
INTERVIEW: 'interview' as TWorksTab,
10-
}
4+
STORY: 'story',
5+
BASIC: 'basic',
6+
TECHSTACKS: 'techstacks',
7+
COMMUNITY: 'community',
8+
MILESTONE: 'milestone',
9+
INTERVIEW: 'interview',
10+
} as Record<Uppercase<TWorksTab>, TWorksTab>
1111

1212
export const WORKS_TAB_ITEMS = [
1313
{
@@ -41,10 +41,10 @@ export const WORKS_TAB_ITEMS = [
4141
]
4242

4343
export const BLOG_TAB = {
44-
DIGEST: 'digest' as TBlogTab,
45-
AUTHOR: 'author' as TBlogTab,
46-
FEEDS: 'feeds' as TBlogTab,
47-
}
44+
DIGEST: 'digest',
45+
AUTHOR: 'author',
46+
FEEDS: 'feeds',
47+
} as Record<Uppercase<TBlogTab>, TBlogTab>
4848

4949
export const BLOG_TAB_ITEMS = [
5050
{

utils/constant/tag.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { TTagMode } from '@/spec'
22

33
const TAG_MODE = {
4-
DEFAULT: 'default' as TTagMode,
5-
LABEL: 'label' as TTagMode,
6-
}
4+
DEFAULT: 'default',
5+
LABEL: 'label',
6+
} as Record<Uppercase<TTagMode>, TTagMode>
77

88
export default TAG_MODE

0 commit comments

Comments
 (0)