Skip to content

Commit

Permalink
fix: isObject type inference
Browse files Browse the repository at this point in the history
  • Loading branch information
nusohiro committed Jan 29, 2025
1 parent a280dd5 commit 54dd104
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/typed/isObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ import { isTagged } from 'radashi'
* ```
* @version 12.1.0
*/
export function isObject(value: unknown): value is object {
export function isObject<T>(value: T): value is Exclude<T, Function> & object {
return isTagged(value, '[object Object]')
}
10 changes: 10 additions & 0 deletions tests/typed/isObject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,14 @@ describe('isObject', () => {
const result = _.isObject([1, 2, 3])
expect(result).toBeFalsy()
})
test('correctly distinguishes between function and object in a union type', () => {
const returnSomething: () => (() => void) | { a: string } = () => ({ a: 'a' })
const something = returnSomething()

if (_.isObject(something)) {
expect(something).toEqual({ a: 'a' })
} else {
expect(typeof something).toBe('function')
}
})
})

0 comments on commit 54dd104

Please sign in to comment.