0
# External Object Type Checking
1
2
Type checking for objects from external libraries. Currently supports Long objects from the 'long' npm package.
3
4
## Capabilities
5
6
### Long Object Type Checking
7
8
Returns true if value is a LongObject from the 'long' npm package.
9
10
```typescript { .api }
11
/**
12
* Returns true if val is LongObject
13
* LongObject is from npm package `long`
14
* @param obj - Value to check
15
* @returns Type guard indicating if value is LongObject
16
*/
17
function isLongObject(obj?: unknown): obj is LongObject;
18
19
interface LongObject {
20
high: number;
21
low: number;
22
}
23
```
24
25
**Usage Example:**
26
27
```typescript
28
import { isLongObject } from "is-type-of";
29
import Long from "long"; // npm install long
30
31
const longValue = Long.fromNumber(123456789);
32
const regularObject = { high: 1, low: 2 };
33
34
isLongObject(longValue); // => true
35
isLongObject(regularObject); // => true (if it has high/low number properties)
36
isLongObject({ high: "1", low: 2 }); // => false (high must be number)
37
isLongObject({}); // => false
38
```
39
40
## Long.js Integration
41
42
The [long.js](https://github.com/dcodeIO/long.js) library provides support for 64-bit integers in JavaScript. Long objects have the following structure:
43
44
```typescript
45
interface LongObject {
46
high: number; // The high 32 bits
47
low: number; // The low 32 bits
48
}
49
```
50
51
The `isLongObject` function validates that an object:
52
1. Is an object (not null)
53
2. Has both `high` and `low` properties
54
3. Both properties are numbers
55
56
**Note**: This function checks for the structural shape of Long objects, not strict instanceof checking. Any object with the correct `high` and `low` number properties will pass this check.
57
58
## Working with Long Objects
59
60
```typescript
61
import { isLongObject } from "is-type-of";
62
import Long from "long";
63
64
function processLongValue(value: unknown) {
65
if (isLongObject(value)) {
66
// value is now typed as LongObject
67
console.log(`High: ${value.high}, Low: ${value.low}`);
68
69
// If using with long.js library
70
if (Long.isLong(value)) {
71
// It's an actual Long instance
72
console.log(`As string: ${value.toString()}`);
73
}
74
}
75
}
76
77
// Examples
78
const longValue = Long.fromString("9223372036854775807");
79
const longLike = { high: 2147483647, low: -1 };
80
81
processLongValue(longValue); // Both high/low properties and Long methods
82
processLongValue(longLike); // Just high/low properties
83
```
84
85
## Type Definitions
86
87
```typescript { .api }
88
interface LongObject {
89
high: number;
90
low: number;
91
}
92
```