Skip to content

Commit

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

Fix `IsNegative<number & { a: 1 }>` should returns `boolean`
5 changes: 5 additions & 0 deletions packages/type-plus/src/numeric/is_negative.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ it('returns false if T is intersection of non-negative numbers', () => {
testType.false<IsNegative<-1n & { a: 1 }>>(true)
})

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

it('distributes over union type', () => {
testType.equal<IsNegative<1 | string>, false>(true)
testType.equal<IsNegative<-1 | string>, boolean>(true)
Expand Down
10 changes: 6 additions & 4 deletions packages/type-plus/src/numeric/is_negative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export type IsNegative<T, $O extends IsNegative.$Options = {}> = IsBigint<T, {
? (
R extends $Then
? IsNegative._Negative<T, number, $O>
: $ResolveBranch<T,$O, [$Else]>
: $ResolveBranch<T, $O, [$Else]>
)
: never
)
Expand All @@ -39,11 +39,13 @@ export type IsNegative<T, $O extends IsNegative.$Options = {}> = IsBigint<T, {
export namespace IsNegative {
export type $Options = $Equality.$Options
export type $Branch<$O extends $Options = {}> = $Equality.$Branch<$O>
export type _Negative<T, U extends number | bigint, $O extends IsNegative.$Options> = T extends U
export type _Negative<T, U extends number | bigint, $O extends IsNegative.$Options> = T extends U & infer R
? (
`${T}` extends `-${string}`
? $ResolveBranch<T,$O, [$Then]>
: U extends T ? $ResolveBranch<T,$O, [$Then]> | $ResolveBranch<T,$O, [$Else]> : $ResolveBranch<T,$O, [$Else]>
? $ResolveBranch<T, $O, [$Then]>
: U extends T ? $ResolveBranch<T, $O, [$Then]> | $ResolveBranch<T, $O, [$Else]>
: [T, R] extends [R, T] ? $ResolveBranch<T, $O, [$Else]>
: $ResolveBranch<number, $O, [$Then]> | $ResolveBranch<T, $O, [$Else]>
)
: never
}

0 comments on commit cc94afd

Please sign in to comment.