generated from basics/blueprint-npm-module
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(jstree): typings for helper functions
- Loading branch information
Showing
3 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |