# ts-serde

> 🎶 Typed Serialization and Deserialization

Latest version **1.0.9** (published 2025-03-14) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

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

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.9 |
| Published | 2025-03-14 |
| First published | 2023-11-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM |
| Dependencies | 1 |
| Unpacked size | 10.8 KB |
| Known vulnerabilities | 0 (+6 in 1 direct dependencies) |
| Install scripts | no |
| Author | jill64 |
| Maintainers | jill64 |
| Keywords | deserialize, serde, serialize, type-safe |

## Links

- npm: https://www.npmjs.com/package/ts-serde
- Repository: https://github.com/jill64/ts-serde
- Homepage: https://github.com/jill64/ts-serde#readme
- Issues: https://github.com/jill64/ts-serde/issues
- npm.io page: https://npm.io/package/ts-serde

## Dependencies (1)

- [devalue](https://npm.io/package/devalue.md) 5.1.1

## Recent versions

- 1.0.9 (latest) — 2025-03-14
- 1.0.8 — 2024-09-25
- 1.0.7 — 2024-07-10
- 1.0.6 — 2024-04-20
- 1.0.5 — 2024-04-19
- 1.0.4 — 2024-04-10
- 1.0.3 — 2024-02-17
- 1.0.2 — 2023-12-19
- 1.0.0 — 2023-12-05
- 0.4.0 — 2023-12-04
- 0.3.1 — 2023-11-30
- 0.3.0 — 2023-11-28
- 0.2.1 — 2023-11-28
- 0.2.0 — 2023-11-28
- 0.1.0 — 2023-11-28
- … 1 more at https://npm.io/package/ts-serde/versions

## README

<!----- BEGIN GHOST DOCS HEADER ----->

# ts-serde

<!----- BEGIN GHOST DOCS BADGES ----->

<a href="https://npmjs.com/package/ts-serde"><img src="https://img.shields.io/npm/v/ts-serde" alt="npm-version" /></a> <a href="https://npmjs.com/package/ts-serde"><img src="https://img.shields.io/npm/l/ts-serde" alt="npm-license" /></a> <a href="https://npmjs.com/package/ts-serde"><img src="https://img.shields.io/npm/dm/ts-serde" alt="npm-download-month" /></a> <a href="https://npmjs.com/package/ts-serde"><img src="https://img.shields.io/bundlephobia/min/ts-serde" alt="npm-min-size" /></a> <a href="https://github.com/jill64/ts-serde/actions/workflows/ci.yml"><img src="https://github.com/jill64/ts-serde/actions/workflows/ci.yml/badge.svg" alt="ci.yml" /></a>

<!----- END GHOST DOCS BADGES ----->

🎶 Typed Serialization and Deserialization

<!----- END GHOST DOCS HEADER ----->

This library is a type-safe serialization/deserialization library inspired by [`serde.rs`](https://serde.rs).  
It contains the basic abstract types, some primitive functions, and object functions.

## Installation

```sh
npm i ts-serde
```

## Types

```ts
import { Serde } from 'ts-serde'
import { Serialize, Deserialize } from 'ts-serde/types'
```

```ts
type Serialize<T> = (val: T) => string

type Deserialize<T> = (str: string) => T

type Serde<T> = {
  serialize: Serialize<T>
  deserialize: Deserialize<T>
}
```

## Primitive

Simple implementation using standard constructors.

```js
import { string, number, boolean, bigint } from 'ts-serde/primitive'
```

| Type                                  | serialize | deserialize    |
| ------------------------------------- | --------- | -------------- |
| [string](./src/primitive/string.ts)   | `String`  | `String`       |
| [number](./src/primitive/number.ts)   | `String`  | `Number`       |
| [bigint](./src/primitive/bigint.ts)   | `String`  | `BigInt`       |
| [boolean](./src/primitive/boolean.ts) | `String`  | `x === 'true'` |
| [integer](./src/primitive/integer.ts) | `String`  | `parseInt`     |

## Enum

```js
import { enums } from 'ts-serde/object'

const e = enums(['foo', 'bar', 'baz'])

e.serialize('foo') // => 'foo'
e.deserialize('foo') // => 'foo'

e.deserialize('qux') // => To Throw Error

const withFallback = enums(['foo', 'bar', 'baz'], 'fallback')

withFallback.deserialize('qux') // => 'fallback'
```

## Object

The object conversion methods are `JSON` and [`devalue`](https://github.com/Rich-Harris/devalue).

To add a type guard inline

```ts
import { json } from 'ts-serde/object'

const j = json(
  (x): x is { key: string } =>
    // ... Type Guard
)

j.serialize({ key: 'value' }) // => '{"key":"value"}'
j.deserialize('') // => To Throw Error
```

Use `type` to add a type guard instead `interface` for external type definition

```js
import { json } from 'ts-serde/object'

type Settings = {
    v: string;
    lwm: {
        expanded: boolean
    }
}

const j =json<Settings>(
  (x): x is Settings =>
    // ... Type Guard
)
```

`devalue` supports more types than JSON.

```ts
import { devalue } from 'ts-serde/object'

const d = devalue(
  (x): x is Set<Date> =>
    // ... Type Guard
    ,
    null // fallback value
)

d.serialize(new Set([new Date()]))
// => '[["Set",1],["Date","20XX-01-01T00:00:00.000Z"]]'

d.deserialize('') // => null (fallback value)
```

<!----- BEGIN GHOST DOCS FOOTER ----->

## License

[MIT](LICENSE)

<!----- END GHOST DOCS FOOTER ----->

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