# ts-extras

> Essential utilities for TypeScript projects

Latest version **1.3.0** (published 2026-07-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install ts-extras
pnpm add ts-extras
yarn add ts-extras
bun add ts-extras
```

## Health

**Score 60/100 (C)** — status: active.

Positive: esm support; no vulnerabilities; recently updated; high maintenance score.

Warnings: low downloads; no types.

## Facts

| | |
|---|---|
| Version | 1.3.0 |
| Published | 2026-07-17 |
| First published | 2021-10-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM |
| Node | >=20 |
| Dependencies | 1 |
| Unpacked size | 86.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 807 |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | typescript, ts, type, types, utility, utilities, util, extra, extras, guard, guards, helpers |

## Links

- npm: https://www.npmjs.com/package/ts-extras
- Repository: https://github.com/sindresorhus/ts-extras
- Homepage: https://github.com/sindresorhus/ts-extras#readme
- Issues: https://github.com/sindresorhus/ts-extras/issues
- Funding: https://github.com/sponsors/sindresorhus
- npm.io page: https://npm.io/package/ts-extras

## Dependencies (1)

- [type-fest](https://npm.io/package/type-fest.md) ^5.0.1

## Alternatives

- [lodash.assign](https://npm.io/package/lodash.assign.md) — 2.3M weekly downloads
- [lodash.chunk](https://npm.io/package/lodash.chunk.md) — 1.8M weekly downloads
- [react-native-ios-utilities](https://npm.io/package/react-native-ios-utilities.md) — 138.5K weekly downloads
- [@technically/lodash](https://npm.io/package/@technically/lodash.md) — 50.9K weekly downloads
- [@fluid-topics/ft-icon](https://npm.io/package/@fluid-topics/ft-icon.md) — 20.6K weekly downloads

## Recent versions

- 1.3.0 (latest) — 2026-07-17
- 1.2.0 — 2026-07-01
- 1.1.0 — 2026-06-27
- 1.0.0 — 2026-04-17
- 0.19.0 — 2026-03-23
- 0.18.0 — 2026-03-22
- 0.17.0 — 2026-02-02
- 0.16.1 — 2025-12-23
- 0.16.0 — 2025-09-22
- 0.15.0 — 2025-08-15
- 0.14.0 — 2025-04-08
- 0.13.0 — 2024-08-24
- 0.12.0 — 2024-07-26
- 0.11.0 — 2022-08-16
- 0.10.2 — 2022-04-12
- … 14 more at https://npm.io/package/ts-extras/versions

## README

# ts-extras [![](https://img.shields.io/badge/unicorn-approved-ff69b4.svg)](https://giphy.com/gifs/illustration-rainbow-unicorn-26AHG5KGFxSkUWw1i) [![npm dependents](https://badgen.net/npm/dependents/ts-extras)](https://www.npmjs.com/package/ts-extras?activeTab=dependents) [![npm downloads](https://badgen.net/npm/dt/ts-extras)](https://www.npmjs.com/package/ts-extras)

> Essential utilities for TypeScript projects

*Ideas for additional **essential** utilities welcome. Type-only utilities belong in [type-fest](https://github.com/sindresorhus/type-fest).*

## Install

```sh
npm install ts-extras
```

## Usage

```js
import {isDefined} from 'ts-extras';

[1, undefined, 2].filter(isDefined);
//=> [1, 2]
```

## API

**General**

- [`asWritable`](source/as-writable.ts) - Cast the given value to be [`Writable`](https://github.com/sindresorhus/type-fest/blob/main/source/writable.d.ts).
- [`objectUpdate`](source/object-update.ts) - Apply a type-checked partial update to an object in place.
- [`safeCastTo`](source/safe-cast-to.ts) - Constrain a value to the given type safely.

**Type guard**

- [`isDefined`](source/is-defined.ts) - Check whether a value is defined (not `undefined`).
- [`isEqualType`](source/is-equal-type.ts) - Check if two types are equal at compile time.
- [`isPresent`](source/is-present.ts) - Check whether a value is present (not `null` nor `undefined`).
- [`isPropertyDefined`](source/is-property-defined.ts) - Check whether a specific property of a value is defined (not `undefined`).
- [`isPropertyPresent`](source/is-property-present.ts) - Check whether a specific property of a value is present (not `null` nor `undefined`).
- [`isEmpty`](source/is-empty.ts) - Check whether an array is empty.
- [`isFinite`](source/is-finite.ts) - A strongly-typed version of `Number.isFinite()`.
- [`isInfinite`](source/is-infinite.ts) - Check whether a value is infinite.
- [`isInteger`](source/is-integer.ts) - A strongly-typed version of `Number.isInteger()`.
- [`isSafeInteger`](source/is-safe-integer.ts) - A strongly-typed version of `Number.isSafeInteger()`.
- [`keyIn`](source/key-in.ts) - Check if a key is in an object and narrow the key to the object's keys.
- [`not`](source/not.ts) - Invert a type predicate function.
- [`objectHasIn`](source/object-has-in.ts) - Check if an object has a property (including inherited) and narrow the object type.
- [`assert`](source/assert.ts) - Assert that the given condition is truthy.
- [`assertDefined`](source/assert-defined.ts) - Assert that the given value is defined, meaning it is not `undefined`.
- [`assertPresent`](source/assert-present.ts) - Assert that the given value is present (non-nullable), meaning it is neither `null` nor `undefined`.
- [`assertError`](source/assert-error.ts) - Assert that the given value is an `Error`.
- [`assertNever`](source/assert-never.ts) - Assert that a code path is unreachable.

**Improved builtin**

- [`arrayAt`](source/array-at.ts) - A strongly-typed version of `Array#at()` with improved tuple support (supports `-1` and positive literal indices for tuples).
- [`arrayConcat`](source/array-concat.ts) - A strongly-typed version of `Array#concat()` that properly handles arrays of different types.
- [`arrayJoin`](source/array-join.ts) - A strongly-typed version of `Array#join()` that preserves literal string types.
- [`arrayFirst`](source/array-first.ts) - Return the first item of an array with stronger typing for tuples.
- [`arrayIncludes`](source/array-includes.ts) - A strongly-typed version of `Array#includes()` that properly acts as a type guard.
- [`arrayLast`](source/array-last.ts) - Return the last item of an array with stronger typing for tuples.
- [`objectAssign`](source/object-assign.ts) - A strongly-typed version of `Object.assign()`.
- [`objectKeys`](source/object-keys.ts) - A strongly-typed version of `Object.keys()`.
- [`objectValues`](source/object-values.ts) - A strongly-typed version of `Object.values()`.
- [`objectEntries`](source/object-entries.ts) - A strongly-typed version of `Object.entries()`.
- [`objectFromEntries`](source/object-from-entries.ts) - A strongly-typed version of `Object.fromEntries()`.
- [`objectMapValues`](source/object-map-values.ts) - Map over an object's values while preserving strongly-typed keys.
- [`objectHasOwn`](source/object-has-own.ts) - A strongly-typed version of `Object.hasOwn()`.
- [`setHas`](source/set-has.ts) - A strongly-typed version of `Set#has()` that properly acts as a type guard.
- [`stringSplit`](source/string-split.ts) - A strongly-typed version of `String#split()` that returns a tuple for literal strings.

## FAQ

#### What is the difference between `keyIn`, `objectHasIn`, and `objectHasOwn`?

These functions solve different problems despite all checking property existence:

**`keyIn`** - **Key narrowing** for union types:
- Uses the `in` operator, checking the prototype chain
- Narrows the *key* variable to only keys that exist in the object
- Best for: "Which of these possible keys actually exists?"
- Guards against `__proto__` and `constructor` for security

**`objectHasIn`** - **Object narrowing** with prototype chain:
- Uses the `in` operator, checking the prototype chain
- Narrows the *object* type to include the checked property
- Best for: "Can I safely access this property (including inherited)?"
- Guards against `__proto__` and `constructor` for security

**`objectHasOwn`** - **Object narrowing** for own properties:
- Uses `Object.hasOwn()`, checking only own properties
- Narrows the *object* type to include the checked property
- Best for: "Can I safely access this own property on this object?"

```typescript
// keyIn - narrows the key (prototype chain)
const key = 'foo' as 'foo' | 'bar' | 'baz';
if (keyIn(object, key)) {
	// `key` is now: 'foo' | 'bar' (only existing keys)
	console.log(object[key]); // Safe
}

// objectHasIn - narrows the object (prototype chain)
const data: unknown = {foo: 1};
if (objectHasIn(data, 'toString')) {
	// `data` is now: unknown & {toString: unknown}
	console.log(data.toString); // Safe (inherited method)
}

// objectHasOwn - narrows the object (own properties only)
if (objectHasOwn(data, 'foo')) {
	// `data` is now: unknown & {foo: unknown}
	console.log(data.foo); // Safe (own property)
}
```

#### What is the difference between this and `type-fest`?

The `type-fest` package contains only types, meaning they are only used at compile-time and nothing is ever compiled into actual JavaScript code. This package contains functions that are compiled into JavaScript code and used at runtime.

## Related

- [type-fest](https://github.com/sindresorhus/type-fest) - A collection of essential TypeScript types
- [is](https://github.com/sindresorhus/is) - Type guards for any situation
- [camelcase-keys](https://github.com/sindresorhus/camelcase-keys) - Runtime transformation of object properties to camel-case (like `type-fest`'s `CamelCasedPropertiesDeep`)

---
_Source: https://npm.io/package/ts-extras · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
