# define-accessors

> define accessors for an object on another object

Latest version **1.1.1** (published 2022-03-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install define-accessors
pnpm add define-accessors
yarn add define-accessors
bun add define-accessors
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2022-03-13 |
| First published | 2021-12-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 18.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | stagas |
| Maintainers | stagas |
| Keywords | accessors, getters, setters |

## Links

- npm: https://www.npmjs.com/package/define-accessors
- Repository: https://git@github.com/stagas/define-accessors
- Homepage: https://github.com/stagas/define-accessors#readme
- Issues: https://github.com/stagas/define-accessors/issues
- npm.io page: https://npm.io/package/define-accessors

## Recent versions

- 1.1.1 (latest) — 2022-03-13
- 1.1.0 — 2021-12-04
- 1.0.0 — 2021-12-03

## README

<h1 align="center">define-accessors</h1>

<p align="center">
define accessors for an object on another object
</p>

<p align="center">
   <a href="#install">        🔧 <strong>Install</strong></a>
 · <a href="#example">        🧩 <strong>Example</strong></a>
 · <a href="#api">            📜 <strong>API docs</strong></a>
 · <a href="https://github.com/stagas/define-accessors/releases"> 🔥 <strong>Releases</strong></a>
 · <a href="#contribute">     💪🏼 <strong>Contribute</strong></a>
 · <a href="https://github.com/stagas/define-accessors/issues">   🖐️ <strong>Help</strong></a>
</p>

***

## Install

```sh
$ npm i define-accessors
```

## API

<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

#### Table of Contents

*   [defineAccessors](#defineaccessors)
    *   [Parameters](#parameters)

### defineAccessors

[src/index.ts:59-72](https://github.com/stagas/define-accessors/blob/95f6e9401b67076377588e17927a3a3c968be44f/src/index.ts#L59-L72 "Source code on GitHub")

Defines accessors for a source object on a target object.

Example; all values reflected on source:

```ts
const target = {}
const source = {
  foo: 'a prop',
  bar: 'another',
  zoo: 10,
}
const typed = defineAccessors(target, source)
expect(typed).toBe(target)
expect(typed.foo).toBe(source.foo)
expect(typed.bar).toBe(source.bar)
expect(typed.zoo).toBe(source.zoo)

typed.foo = 'something else'
expect(typed.foo).toEqual('something else')
expect(source.foo).toEqual('something else')
```

Example; all values reflected on `other` using a custom property descriptor factory:

```ts
const target = {}
const source = {
  foo: 'a prop',
}
const other: Record<string, unknown> = {
  foo: 'something else',
}
const typed = defineAccessors(target, source, (key: string) => ({
  enumerable: true,
  get() {
    return other[key]
  },
  set(value: never) {
    other[key] = value
  },
}))
expect(typed).toBe(target)
expect(typed.foo).toBe(other.foo)
```

#### Parameters

*   `target` **T** The target object to define accessors on
*   `source` **S** The source object where the actual values are
*   `propertyDescriptorFactory` **function (key: any, source: S): PropertyDescriptor** A function that returns a custom property descriptor for the given key (optional, default `createPropertyDescriptor`)

Returns **any** The target object but with its type intersected with the source's type

## Contribute

[Fork](https://github.com/stagas/define-accessors/fork) or
[edit](https://github.dev/stagas/define-accessors) and submit a PR.

All contributions are welcome!

## License

MIT © 2021
[stagas](https://github.com/stagas)

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