You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+54-4Lines changed: 54 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,59 @@
7
7
8
8
# extension-methods
9
9
10
-
With this library, you can create 'proxy references' for your objects and access many methods that actually doesn't exists in them!
10
+
This library brings to Typescript a really useful functionality that other languages already have, like C# or Kotlin: extension methods!
11
+
12
+
Extension methods are a way to declare static methods that are aeasily referenced in specific types through the dot notation, like if they belong to a class itself! Let me give you a example:
13
+
14
+
```typescript
15
+
classStringExtensions {
16
+
@ExtensionMethod
17
+
static size(str:string) {
18
+
returnstr.length;
19
+
}
20
+
}
21
+
22
+
interfaceString {
23
+
Size():number;
24
+
}
25
+
26
+
'my-string'.size();
27
+
```
28
+
29
+
Easy enough, right? If you already know this feature, I'm sure you miss it in Javascript/Typescript, but with this library no-more!
30
+
31
+
Also, this library works with two modes:
32
+
33
+
- AST Transformer
34
+
- Proxy reference
35
+
36
+
Each of them are explained below and you can use the one that fits you better!
37
+
38
+
# AST Transformer mode
39
+
40
+
In this mode, everything is done at transpiling time, making you able to access each of the extensions methods just by importing them where you need, like this:
41
+
42
+
```typescript
43
+
import'./string-extensions';
44
+
45
+
console.log('my-strign'.size());
46
+
```
47
+
48
+
Due to typescript limitation, not only you have to declare the extension method, but also the additional methods to merge into the type you want to add them, like the example more above. Now, to apply this transformation, you need to use a compiler command that supports adding ast transformers to the pipe. We suggest you to use **nest build.**
49
+
50
+
You'll have to add the following plugin to your **nest-cli.json**:
51
+
52
+
```typescript
53
+
"plugins": [
54
+
"extension-methods/plugin"
55
+
],
56
+
```
57
+
58
+
That's it! this is enough to get the extensions you want!
59
+
60
+
# Proxy reference mode
61
+
62
+
In this mode, you can create 'proxy references' for your objects and access many methods that actually doesn't exists in them!
11
63
This is pretty useful in the following scenarios:
12
64
13
65
- where you need to create a method that returns an object with a lot of methods, but the code that'll use that result will only access a few of them;
@@ -16,8 +68,6 @@ This is pretty useful in the following scenarios:
16
68
Depending on the number of methods you'll proxy through **extension-methods**, you can achieve a 99% faster operation than a simple **new Class()**
17
69
<br>
18
70
19
-
## How to use it
20
-
21
71
First, you need to obtain your extension object, like this:
22
72
<br>
23
73
@@ -116,5 +166,5 @@ And that's it, it'll just work!
116
166
## Important
117
167
118
168
- If some method exists in the original object and also is declared in the Extender, the original method will be used;
119
-
- **extension-methods** can't be used with primitive values like **string**, **number** and **boolean**;
169
+
- **extension-methods** can't be used in proxy mode with primitive values like **string**, **number** and **boolean**, but it'll work with the ast mode!
120
170
- **extend** will naturally returns a type that is a join between the real object and the extension methods declared, but it is recommendable, if you want a cleaner type or to return such value as a result of a function, to create an interface that represent it, as you can see in the examples above;
0 commit comments