Skip to content

Commit

Permalink
feat(utils): 新增 asRequiredDeep 将给定的值 RequiredDeep
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Feb 26, 2022
1 parent f7f7463 commit feb381a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/utils/asRequiredDeep.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { asRequiredDeep } from './asRequiredDeep'

describe('asRequiredDeep', () => {
test('ok', () => {
const obj: {
x?: string
y?: {
z?: number
x2: [
{
y2?: {
z2: number
}
},
]
}
} = {
y: {
z: 2,
x2: [{}],
},
}
// @ts-expect-error
const x = obj.y.z
expect(x).toBe(2)

const obj2 = asRequiredDeep(obj)
const x2 = obj2.y.z
expect(x2).toBe(2)
})
})
10 changes: 10 additions & 0 deletions src/utils/asRequiredDeep.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { RequiredDeep } from '../types'

/**
* 将给定的值 `RequiredDeep` 化。
*
* @param value 值
*/
export function asRequiredDeep<T>(value: T): RequiredDeep<T> {
return value as any
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
export * from 'lodash-uni'

// @index(['./**/*.ts', '!./**/*.{test,perf}.*'], f => `export * from '${f.path}'`)
export * from './asRequiredDeep'
export * from './base64'
export * from './bindEvent'
export * from './Calculator'
Expand Down

0 comments on commit feb381a

Please sign in to comment.