or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

assertions.mdasync.mdcollections.mdindex.mdnumbers.mdobjects.mdprimitives.mdstrings.mdtyped-arrays.mdvalidation.mdweb-apis.md

objects.mddocs/

0

# Objects and Built-in Types

1

2

Type checking for JavaScript built-in objects including arrays, functions, dates, regular expressions, maps, sets, errors, and other object types.

3

4

## Capabilities

5

6

### Array Type Checking

7

8

Check if a value is an array, with optional validation of all array items.

9

10

```typescript { .api }

11

/**

12

* Check if value is an array, optionally validating all items

13

* @param value - Value to check

14

* @param assertion - Optional function to validate each array item

15

* @returns True if value is array and all items pass assertion (if provided)

16

*/

17

function isArray<T = unknown>(value: unknown, assertion?: (value: T) => value is T): value is T[];

18

```

19

20

**Usage Examples:**

21

22

```typescript

23

import is from '@sindresorhus/is';

24

25

is.array([]); // => true

26

is.array([1, 2, 3]); // => true

27

is.array('not array'); // => false

28

29

// With item validation

30

is.array([1, 2, 3], is.number); // => true

31

is.array([1, '2', 3], is.number); // => false

32

is.array(['a', 'b'], is.string); // => true

33

34

// Type guard with validation

35

if (is.array(someValue, is.string)) {

36

// someValue is now typed as string[]

37

console.log(someValue.map(s => s.toUpperCase()));

38

}

39

```

40

41

### Function Type Checking

42

43

Check if a value is a function.

44

45

```typescript { .api }

46

/**

47

* Check if value is a function

48

* @param value - Value to check

49

* @returns True if value is function

50

*/

51

function isFunction(value: unknown): value is Function;

52

```

53

54

**Usage Examples:**

55

56

```typescript

57

import is from '@sindresorhus/is';

58

59

is.function(() => {}); // => true

60

is.function(function() {}); // => true

61

is.function(Math.max); // => true

62

is.function({}); // => false

63

```

64

65

### Object Type Checking

66

67

Check if a value is an object. Note that functions are also considered objects in JavaScript.

68

69

```typescript { .api }

70

/**

71

* Check if value is an object (includes functions)

72

* @param value - Value to check

73

* @returns True if value is object

74

*/

75

function isObject(value: unknown): value is object;

76

```

77

78

**Usage Examples:**

79

80

```typescript

81

import is from '@sindresorhus/is';

82

83

is.object({}); // => true

84

is.object([]); // => true

85

is.object(() => {}); // => true (functions are objects)

86

is.object(new Date()); // => true

87

is.object(null); // => false

88

is.object('string'); // => false

89

```

90

91

### Plain Object Type Checking

92

93

Check if a value is a plain object created by `{}`, `new Object()`, or `Object.create(null)`.

94

95

```typescript { .api }

96

/**

97

* Check if value is a plain object

98

* @param value - Value to check

99

* @returns True if value is plain object

100

*/

101

function isPlainObject<Value = unknown>(value: unknown): value is Record<PropertyKey, Value>;

102

```

103

104

**Usage Examples:**

105

106

```typescript

107

import is from '@sindresorhus/is';

108

109

is.plainObject({}); // => true

110

is.plainObject(Object.create(null)); // => true

111

is.plainObject(new Object()); // => true

112

is.plainObject([]); // => false

113

is.plainObject(new Date()); // => false

114

is.plainObject(() => {}); // => false

115

```

116

117

### Date Type Checking

118

119

Check if a value is a Date object.

120

121

```typescript { .api }

122

/**

123

* Check if value is a Date object

124

* @param value - Value to check

125

* @returns True if value is Date

126

*/

127

function isDate(value: unknown): value is Date;

128

```

129

130

**Usage Examples:**

131

132

```typescript

133

import is from '@sindresorhus/is';

134

135

is.date(new Date()); // => true

136

is.date(new Date('invalid')); // => true (but may be Invalid Date)

137

is.date('2023-01-01'); // => false

138

is.date(1640995200000); // => false

139

```

140

141

### Valid Date Checking

142

143

Check if a value is a valid Date object (not Invalid Date).

144

145

```typescript { .api }

146

/**

147

* Check if value is a valid Date object

148

* @param value - Value to check

149

* @returns True if value is valid Date

150

*/

151

function isValidDate(value: unknown): value is Date;

152

```

153

154

**Usage Examples:**

155

156

```typescript

157

import is from '@sindresorhus/is';

158

159

is.validDate(new Date()); // => true

160

is.validDate(new Date('2023-01-01')); // => true

161

is.validDate(new Date('invalid')); // => false

162

is.validDate('2023-01-01'); // => false

163

```

164

165

### RegExp Type Checking

166

167

Check if a value is a regular expression.

168

169

```typescript { .api }

170

/**

171

* Check if value is a RegExp

172

* @param value - Value to check

173

* @returns True if value is RegExp

174

*/

175

function isRegExp(value: unknown): value is RegExp;

176

```

177

178

**Usage Examples:**

179

180

```typescript

181

import is from '@sindresorhus/is';

182

183

is.regExp(/abc/); // => true

184

is.regExp(new RegExp('abc')); // => true

185

is.regExp('abc'); // => false

186

```

187

188

### Error Type Checking

189

190

Check if a value is an Error object.

191

192

```typescript { .api }

193

/**

194

* Check if value is an Error

195

* @param value - Value to check

196

* @returns True if value is Error

197

*/

198

function isError(value: unknown): value is Error;

199

```

200

201

**Usage Examples:**

202

203

```typescript

204

import is from '@sindresorhus/is';

205

206

is.error(new Error()); // => true

207

is.error(new TypeError()); // => true

208

is.error(new ReferenceError()); // => true

209

is.error('error message'); // => false

210

```

211

212

### Map Type Checking

213

214

Check if a value is a Map object.

215

216

```typescript { .api }

217

/**

218

* Check if value is a Map

219

* @param value - Value to check

220

* @returns True if value is Map

221

*/

222

function isMap<Key = unknown, Value = unknown>(value: unknown): value is Map<Key, Value>;

223

```

224

225

**Usage Examples:**

226

227

```typescript

228

import is from '@sindresorhus/is';

229

230

is.map(new Map()); // => true

231

is.map(new Map([['key', 'value']])); // => true

232

is.map({}); // => false

233

is.map(new Set()); // => false

234

```

235

236

### Set Type Checking

237

238

Check if a value is a Set object.

239

240

```typescript { .api }

241

/**

242

* Check if value is a Set

243

* @param value - Value to check

244

* @returns True if value is Set

245

*/

246

function isSet<T = unknown>(value: unknown): value is Set<T>;

247

```

248

249

**Usage Examples:**

250

251

```typescript

252

import is from '@sindresorhus/is';

253

254

is.set(new Set()); // => true

255

is.set(new Set([1, 2, 3])); // => true

256

is.set([]); // => false

257

is.set(new Map()); // => false

258

```

259

260

### WeakMap Type Checking

261

262

Check if a value is a WeakMap object.

263

264

```typescript { .api }

265

/**

266

* Check if value is a WeakMap

267

* @param value - Value to check

268

* @returns True if value is WeakMap

269

*/

270

function isWeakMap<Key extends object = object, Value = unknown>(value: unknown): value is WeakMap<Key, Value>;

271

```

272

273

**Usage Examples:**

274

275

```typescript

276

import is from '@sindresorhus/is';

277

278

is.weakMap(new WeakMap()); // => true

279

is.weakMap(new Map()); // => false

280

```

281

282

### WeakSet Type Checking

283

284

Check if a value is a WeakSet object.

285

286

```typescript { .api }

287

/**

288

* Check if value is a WeakSet

289

* @param value - Value to check

290

* @returns True if value is WeakSet

291

*/

292

function isWeakSet<T extends object = object>(value: unknown): value is WeakSet<T>;

293

```

294

295

**Usage Examples:**

296

297

```typescript

298

import is from '@sindresorhus/is';

299

300

is.weakSet(new WeakSet()); // => true

301

is.weakSet(new Set()); // => false

302

```

303

304

### WeakRef Type Checking

305

306

Check if a value is a WeakRef object.

307

308

```typescript { .api }

309

/**

310

* Check if value is a WeakRef

311

* @param value - Value to check

312

* @returns True if value is WeakRef

313

*/

314

function isWeakRef<T extends object = object>(value: unknown): value is WeakRef<T>;

315

```

316

317

**Usage Examples:**

318

319

```typescript

320

import is from '@sindresorhus/is';

321

322

const obj = {};

323

is.weakRef(new WeakRef(obj)); // => true

324

is.weakRef(obj); // => false

325

```

326

327

### Class Type Checking

328

329

Check if a value is a class constructor.

330

331

```typescript { .api }

332

/**

333

* Check if value is a class constructor

334

* @param value - Value to check

335

* @returns True if value is class

336

*/

337

function isClass<T = unknown>(value: unknown): value is Class<T>;

338

339

type Class<T, Arguments extends unknown[] = any[]> = Constructor<T, Arguments> & {prototype: T};

340

```

341

342

**Usage Examples:**

343

344

```typescript

345

import is from '@sindresorhus/is';

346

347

class MyClass {}

348

is.class(MyClass); // => true

349

is.class(function() {}); // => false

350

is.class(() => {}); // => false

351

```

352

353

### Direct Instance Checking

354

355

Check if an instance is a direct instance of a class (not inherited).

356

357

```typescript { .api }

358

/**

359

* Check if instance is direct instance of class

360

* @param instance - Instance to check

361

* @param class_ - Class to check against

362

* @returns True if instance is direct instance of class

363

*/

364

function isDirectInstanceOf<T>(instance: unknown, class_: Class<T>): instance is T;

365

```

366

367

**Usage Examples:**

368

369

```typescript

370

import is from '@sindresorhus/is';

371

372

class Parent {}

373

class Child extends Parent {}

374

375

const parent = new Parent();

376

const child = new Child();

377

378

is.directInstanceOf(parent, Parent); // => true

379

is.directInstanceOf(child, Parent); // => false

380

is.directInstanceOf(child, Child); // => true

381

```

382

383

### Bound Function Type Checking

384

385

Check if a value is a bound function.

386

387

```typescript { .api }

388

/**

389

* Check if value is a bound function

390

* @param value - Value to check

391

* @returns True if value is bound function

392

*/

393

function isBoundFunction(value: unknown): value is Function;

394

```

395

396

**Usage Examples:**

397

398

```typescript

399

import is from '@sindresorhus/is';

400

401

const fn = function() {};

402

const boundFn = fn.bind(null);

403

const arrowFn = () => {};

404

405

is.boundFunction(boundFn); // => true

406

is.boundFunction(arrowFn); // => true (arrow functions are bound)

407

is.boundFunction(fn); // => false

408

```

409

410

### Property Key Type Checking

411

412

Check if a value can be used as an object property key (string, number, or symbol).

413

414

```typescript { .api }

415

/**

416

* Check if value can be used as property key

417

* @param value - Value to check

418

* @returns True if value is valid property key

419

*/

420

function isPropertyKey(value: unknown): value is PropertyKey;

421

422

type PropertyKey = string | number | symbol;

423

```

424

425

**Usage Examples:**

426

427

```typescript

428

import is from '@sindresorhus/is';

429

430

is.propertyKey('key'); // => true

431

is.propertyKey(42); // => true

432

is.propertyKey(Symbol('key')); // => true

433

is.propertyKey({}); // => false

434

is.propertyKey(null); // => false

435

```

436

437

## Notes

438

439

- Functions are considered objects in JavaScript, so `is.object(fn)` returns `true`

440

- Use `is.plainObject()` to exclude arrays, functions, and other object types

441

- `is.date()` returns `true` for Invalid Date objects; use `is.validDate()` for valid dates only

442

- All object type checks work with TypeScript type guards for compile-time type narrowing