-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(utils): 新增 asRequiredDeep 将给定的值
RequiredDeep
化
- Loading branch information
Showing
3 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters