Skip to content

Commit

Permalink
fix(devOrProd): 类型优化
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Sep 28, 2021
1 parent b611997 commit 449f957
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
9 changes: 9 additions & 0 deletions src/utils/devOrProd.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,13 @@ describe('devOrProd', () => {
),
).toBe(4)
})

test('正确推导类型', () => {
const obj: {
x: 'ppp' | 'ff' | '00'
} = {
x: devOrProd('00', () => 'ff'),
}
expect(obj).toBe(obj)
})
})
8 changes: 4 additions & 4 deletions src/utils/devOrProd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
* @param devValue 开发环境返回的值或调用的函数
* @param prodValue 生产环境返回的值或调用的函数
*/
export function devOrProd<T>(
devValue: T | (() => T),
prodValue: T | (() => T),
): T {
export function devOrProd<R, T extends R, F extends () => R>(
devValue: T | F,
prodValue: T | F,
): R {
const nodeEnv = process.env.NODE_ENV
return !nodeEnv || nodeEnv === 'production' || nodeEnv === 'prod'
? typeof prodValue === 'function'
Expand Down

0 comments on commit 449f957

Please sign in to comment.