Skip to content

Commit

Permalink
fix: hasKeys types fix
Browse files Browse the repository at this point in the history
  • Loading branch information
KiraLT committed Jan 2, 2023
1 parent de7bf0b commit b23c8fb
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
3 changes: 3 additions & 0 deletions spec/guards.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ describe('hasKeys', () => {

if (hasKeys(a, ['a', 'b'])) {
expect(a.a).toBe(a.b)

// @ts-expect-error
expect(a.a.toFixed(1)).toBe('1.0')
}
})

Expand Down
3 changes: 1 addition & 2 deletions src/guards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,7 @@ export function ensureError(value: unknown): Error {
export function hasKeys<T extends unknown, Key extends keyof any>(
obj: T,
keys: ReadonlyArray<Key>
// @ts-ignore
): obj is (T extends { [K in Key]: any } ? Extract<{ [K in Key]: any }, T> : { [K in Key]: unknown }) {
): obj is (T extends { [K in Key]: any } ? Extract<{ [K in Key]: any }, T> : Extract<{ [K in Key]: unknown }, T>) {
if (typeof obj === 'object' && keys.every((v) => v in (obj as any))) {
return true
}
Expand Down

0 comments on commit b23c8fb

Please sign in to comment.