Skip to content

Commit

Permalink
fix: IsInteger with number & { a: 1 }>
Browse files Browse the repository at this point in the history
  • Loading branch information
unional committed Mar 24, 2024
1 parent 9c859cd commit 3d56d1a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/sharp-walls-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"type-plus": patch
---

Fix `IsInteger<number & { a: 1 }>` should returns `boolean`
10 changes: 8 additions & 2 deletions packages/type-plus/src/numeric/is_integer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,14 @@ it('can disable union distribution', () => {
testType.equal<IsInteger<1 | string, { distributive: false }>, false>(true)
})

it('returns true for intersection type', () => {
testType.equal<IsInteger<number & { a: 1 }>, true>(true)
it('returns true for intersection type with integer literals', () => {
testType.equal<IsInteger<1 & { a: 1 }>, true>(true)
testType.equal<IsInteger<-1 & { a: 1 }>, true>(true)
testType.equal<IsInteger<1n & { a: 1 }>, true>(true)
})

it('returns boolean when T is an intersection type with number', () => {
testType.equal<IsInteger<number & { a: 1 }>, boolean>(true)
})

it('works as filter', () => {
Expand Down
5 changes: 3 additions & 2 deletions packages/type-plus/src/numeric/is_integer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ export type IsInteger<T, $O extends IsInteger.$Options = {}> = IsNumber<T, {
? R extends $Then ? (
number extends T
? $ResolveBranch<number, $O, [$Then]> | $ResolveBranch<T, $O, [$Else]>
: T extends number ? (
: T extends number & infer U ? (
`${T}` extends `${number}.${number}`
? $ResolveBranch<T, $O, [$Else]>
: $ResolveBranch<T, $O, [$Then]>
: [T, U] extends [U, T] ? $ResolveBranch<T, $O, [$Then]>
: $ResolveBranch<number, $O, [$Then]> | $ResolveBranch<T, $O, [$Else]>
)
: never
)
Expand Down

0 comments on commit 3d56d1a

Please sign in to comment.