UNPKG

1.46 kBMarkdownView Raw
1<!-- Note: This file is automatically generated from source code comments. Changes made in this file will be overridden. -->
2
3# Function typed
4
5Create a typed-function which checks the types of the arguments and
6can match them against multiple provided signatures. The typed-function
7automatically converts inputs in order to find a matching signature.
8Typed functions throw informative errors in case of wrong input arguments.
9
10See the library [typed-function](https://github.com/josdejong/typed-function)
11for detailed documentation.
12
13
14## Syntax
15
16```js
17math.typed(name, signatures) : function
18math.typed(signatures) : function
19```
20
21### Parameters
22
23Parameter | Type | Description
24--------- | ---- | -----------
25`name` | string | Optional name for the typed-function
26`signatures` | Object&lt;string, function&gt; | Object with one or multiple function signatures
27
28### Returns
29
30Type | Description
31---- | -----------
32function | The created typed-function.
33
34
35## Examples
36
37```js
38// create a typed function with multiple types per argument (type union)
39const fn2 = typed({
40 'number | boolean': function (b) {
41 return 'b is a number or boolean'
42 },
43 'string, number | boolean': function (a, b) {
44 return 'a is a string, b is a number or boolean'
45 }
46})
47
48// create a typed function with an any type argument
49const log = typed({
50 'string, any': function (event, data) {
51 console.log('event: ' + event + ', data: ' + JSON.stringify(data))
52 }
53})
54```
55
56