Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ayecue/overload

Method overloading via decorators
https://github.com/ayecue/overload

overloading

Last synced: 15 days ago
JSON representation

Method overloading via decorators

Awesome Lists containing this project

README

        

# overload

[![overload](https://circleci.com/gh/ayecue/overload.svg?style=svg)](https://circleci.com/gh/ayecue/overload)

Pseudo overload via decorators.

## Installation

```sh
npm install --save pseudo-overload
```

## Example

```ts
import { z } from 'zod';
import { OverloadConstructor, Overload } from 'pseudo-overload';

@OverloadConstructor(
[[z.string(), z.number()], function (str: string, num: number) {
this.propertyA = `${str} - ${num}`;
this.propertyB = 'empty';
}],
[[], function () {
this.propertyA = 'empty';
this.propertyB = 'provided no params'
}]
)
export class MyTestClass {
propertyA: string;
propertyB: string;

constructor();
constructor(str: number, num: string);
constructor(str: string, num: number);
constructor(...args: any[]) { }

foo(str: number): string;
foo(str: string): string;
@Overload(
[[z.string()], function (str: string) {
return `A String ${str}`;
}],
)
foo(...args: any[]): any { }

bar(str: string, num?: number): string;
@Overload(
[[z.string(), z.number()], function (str: string, num: number) {
return `A String ${str} And A Number ${num}`;
}],
[[z.string()], function (str: string | null) {
return `A String ${str}`;
}],
)
bar(...args: any[]): any { }
}
```