# decraft

> Crafting TypeScript decorators easily

Latest version **1.0.1** (published 2023-05-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install decraft
pnpm add decraft
yarn add decraft
bun add decraft
```

## Health

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

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

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2023-05-24 |
| First published | 2023-05-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 25.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Ben Timor |
| Maintainers | bentimor |
| Keywords | decraft, typescript, decorators, javascript, annotations, functions |

## Links

- npm: https://www.npmjs.com/package/decraft
- Repository: https://github.com/BenTimor/Decraft
- Homepage: https://github.com/BenTimor/Decraft#readme
- Issues: https://github.com/BenTimor/Decraft/issues
- npm.io page: https://npm.io/package/decraft

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 1.0.1 (latest) — 2023-05-24
- 1.0.0 — 2023-05-24

## README

# Decraft - Smart TypeScript Decorators

Decraft is a user-friendly npm package that simplifies the creation of TypeScript decorators, significantly enhancing their power and readability. With Decraft, crafting intuitive and efficient decorators is a streamlined and enjoyable experience.

## Starting from the end - How does you use the decorators?

Lets say that we created a decorator named `@myDecorator`. Those are all of the ways that we can use **the same decorator**.

We don't need to create a new decorator for each case, we can just use the same one for all of those cases.

### @myDecorator

The classic way to use decorators. Just put it above a class method.

```typescript
class MyClass {
	@myDecorator
	func(...params) {
		// code
	}
}
```

### @myDecorator(...args)

We can also pass arguments to our decorator.

```typescript
class MyClass {
	@myDecorator(...args)
	func(...params) {
		// code
	}
}
```

### myDecorator(func, args)

> Whooho, This is not a decorator!

You are right. But one of the issues with decorators is that we can't use them in some cases. For example for callbacks or functions outside of a class.

So with decraft, we allow you to use some hacks.

```typescript
const func = myDecorator((...params) => {
	// code
}, ...args);
``` 

or another example:
```typescript
function _func(...params) {
  // code
}

const func = myDecorator(_func, ...args)
```

### myDecorator(...args)(func)

We allow also a different approach for readability.

```typescript
const func = myDecorator(...args)((...params) => {
	// code
});
```

## How to create a decorator?

It's pretty straightforward. We call the `decorator` function and pass as a generic a list of types that will be used for the decorator arguments.

Then we pass a callback which will be called each time we create the function (or the object it exists in). 

It may also return a function, and this function will **replace** the function that the decorator is used on.

```typescript
const  myDecorator  =  decorator<[...DecoratorParamsTypes]>((func, decoratorParams) => {
	// The function that we return here will replace the function that it's being used on
	return (...args:  any[]) => {
		// We're doing nothing, just calling the function
		return  func(...args);
	};

	// If we prefer, we can also return nothing and the decorator function will not replace the function that it's being used on
});
```

## Our best practices

1. We still have several type issues, so we recommend to check that you really got the params you wanted to get.
2. Don't return a function with different type that the one you got. TypeScript not updating the function types right now when using this decorator.
3. When using decorator as a function like this: `myDecorator(func, ...args)` or `myDecorator(...args)(func)` you should set a type to the variable it's assigned to. We return `any` right now as a type.

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