1 |
|
2 |
|
3 | # Function typed
|
4 |
|
5 | Create a typed-function which checks the types of the arguments and
|
6 | can match them against multiple provided signatures. The typed-function
|
7 | automatically converts inputs in order to find a matching signature.
|
8 | Typed functions throw informative errors in case of wrong input arguments.
|
9 |
|
10 | See the library [typed-function](https://github.com/josdejong/typed-function)
|
11 | for detailed documentation.
|
12 |
|
13 |
|
14 | ## Syntax
|
15 |
|
16 | ```js
|
17 | math.typed(name, signatures) : function
|
18 | math.typed(signatures) : function
|
19 | ```
|
20 |
|
21 | ### Parameters
|
22 |
|
23 | Parameter | Type | Description
|
24 | --------- | ---- | -----------
|
25 | `name` | string | Optional name for the typed-function
|
26 | `signatures` | Object<string, function> | Object with one or multiple function signatures
|
27 |
|
28 | ### Returns
|
29 |
|
30 | Type | Description
|
31 | ---- | -----------
|
32 | function | The created typed-function.
|
33 |
|
34 |
|
35 | ## Examples
|
36 |
|
37 | ```js
|
38 | // create a typed function with multiple types per argument (type union)
|
39 | const 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
|
49 | const log = typed({
|
50 | 'string, any': function (event, data) {
|
51 | console.log('event: ' + event + ', data: ' + JSON.stringify(data))
|
52 | }
|
53 | })
|
54 | ```
|
55 |
|
56 |
|