Skip to content

Commit

Permalink
feat(jstree): typings for helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
MrTelanie committed Oct 9, 2021
1 parent 59071b7 commit 5f33b9d
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export const string = '';
export const number = 0;
export const boolean = false;

export class ExpectedReturnType<Right> {

}

type TypedClass<T> = { new(...args: any[]): T };

type GetBoolean<T> = { valueOf(): T & boolean };

type GetNumber<T> = { valueOf(): T, toExponential(): string };

type GetString<T> = { valueOf(): T, toLocaleLowerCase(): string };

type TypeOf<T> = TypedClass<T> | GetNumber<T> | GetString<T> | GetBoolean<T> | T;

declare function typ<T>(type: TypeOf<T>): T;

declare function fun<F>(func: F): F extends (...args: any) => void ? (...args: (Required<Parameters<F>> & Array<any>)) => void : ExpectedReturnType<void>;
declare function fun<F, T>(type: TypeOf<T>, func: F): F extends (...args: any) => T ? (...args: (Required<Parameters<F>> & Array<any>)) => T : ExpectedReturnType<T>;

declare function cls<T>(prototype: T): T extends { constructor?(...args: any): void, [key: string]: unknown } ? new (...args: (Parameters<T['constructor']>)) => T : (new () => T);
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export function fun(type, func) {
let result;
try {
result = func.apply(this);

if (activeArgList && activeArgIndex < activeArgList.length) {
throw new Error(`wrong argument length? expected: ${activeArgIndex} is: ${activeArgList.length}`);
}
} finally {
activeArgList = undefined;
activeArgIndex = -999;
Expand Down
69 changes: 69 additions & 0 deletions test/types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { assert } from 'chai';
import { cls, fun, number, string, boolean, typ } from '../src';

describe('type safety tests', () => {

const string1 = typ(string);
const string2 = typ(String);
const string3 = typ('hallo');

const number1 = typ(number);
const number2 = typ(Number);
const number3 = typ(55);

const bool1 = typ(boolean);
const bool2 = typ(Boolean);
const bool3 = typ(true);

const C = cls({
// h: typ(string),
constructor: fun((hello = typ(string)) => {
// this.h = hello;
}),
hallo: fun(string, (hello = typ(string)) => {

return hello;
})
});

const fun1 = fun(string, (str = typ(string), num = typ(number)) => {

return str;
});

const c = new C('blub');

it('tests valid typings', () => {

const hello = fun1('hello', 2);

assert.equal(hello, 'hello');

const world = c.hallo('world');

assert.equal(world, 'world');
});

it('throw errors on invalid typings', () => {
const funBroken = fun(number, (str = typ(string), blub = typ(number)) => {

return 'bar';
});

assert.throws(() => {
funBroken('1', 2);
});

assert.throws(() => {
fun1('', 2, 3);
});

assert.throws(() => {
c.foo('');
});

assert.throws(() => {
const c2 = new C(1);
});
});
});

0 comments on commit 5f33b9d

Please sign in to comment.