pseudo-overload
TypeScript icon, indicating that this package has built-in type declarations

0.2.1 • Public • Published

overload

overload

Pseudo overload via decorators.

Installation

npm install --save pseudo-overload

Example

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

@OverloadConstructor<MyTestClass>(
  [[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<MyTestClass, string>(
    [[z.string()], function (str: string) {
      return `A String ${str}`;
    }],
  )
  foo(...args: any[]): any { }

  bar(str: string, num?: number): string;
  @Overload<MyTestClass, string>(
    [[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 { }
}

Package Sidebar

Install

npm i pseudo-overload

Weekly Downloads

2

Version

0.2.1

License

MIT

Unpacked Size

9.63 kB

Total Files

5

Last publish

Collaborators

  • ayecue