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: docs/key-concept.md
+40-44Lines changed: 40 additions & 44 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -52,6 +52,44 @@ This forces you to break down your application into small pieces, making it easy
52
52
53
53
Learn more about this in [plugin](/essential/plugin.html).
54
54
55
+
## Method Chaining
56
+
Elysia code should always use **method chaining**.
57
+
58
+
As Elysia type system is complex, every methods in Elysia returns a new type reference.
59
+
60
+
**This is important** to ensure type integrity and inference.
61
+
62
+
```typescript twoslash
63
+
import { Elysia } from'elysia'
64
+
65
+
newElysia()
66
+
.state('build', 1)
67
+
// Store is strictly typed // [!code ++]
68
+
.get('/', ({ store: { build } }) =>build)
69
+
// ^?
70
+
.listen(3000)
71
+
```
72
+
73
+
In the code above, **state** returns a new **ElysiaInstance** type, adding a typed `build` property.
74
+
75
+
### Don't use Elysia without method chaining
76
+
Without using method chaining, Elysia doesn't save these new types, leading to no type inference.
77
+
78
+
```typescript twoslash
79
+
// @errors: 2339
80
+
import { Elysia } from'elysia'
81
+
82
+
const app =newElysia()
83
+
84
+
app.state('build', 1)
85
+
86
+
app.get('/', ({ store: { build } }) =>build)
87
+
88
+
app.listen(3000)
89
+
```
90
+
91
+
We recommend to <u>**always use method chaining**</u> to provide an accurate type inference.
92
+
55
93
## Scope
56
94
By default, event/life-cycle in each instance is isolated from each other.
57
95
@@ -99,44 +137,6 @@ This forces you to think about the scope of each property, preventing you from a
99
137
100
138
Learn more about this in [scope](/essential/plugin.html#scope).
101
139
102
-
## Method Chaining
103
-
Elysia code should always use **method chaining**.
104
-
105
-
As Elysia type system is complex, every methods in Elysia returns a new type reference.
106
-
107
-
**This is important** to ensure type integrity and inference.
108
-
109
-
```typescript twoslash
110
-
import { Elysia } from'elysia'
111
-
112
-
newElysia()
113
-
.state('build', 1)
114
-
// Store is strictly typed // [!code ++]
115
-
.get('/', ({ store: { build } }) =>build)
116
-
// ^?
117
-
.listen(3000)
118
-
```
119
-
120
-
In the code above, **state** returns a new **ElysiaInstance** type, adding a typed `build` property.
121
-
122
-
### ❌ Don't: Use Elysia without method chaining
123
-
Without using method chaining, Elysia doesn't save these new types, leading to no type inference.
124
-
125
-
```typescript twoslash
126
-
// @errors: 2339
127
-
import { Elysia } from'elysia'
128
-
129
-
const app =newElysia()
130
-
131
-
app.state('build', 1)
132
-
133
-
app.get('/', ({ store: { build } }) =>build)
134
-
135
-
app.listen(3000)
136
-
```
137
-
138
-
We recommend to <u>**always use method chaining**</u> to provide an accurate type inference.
139
-
140
140
## Dependency
141
141
By default, each instance will be re-executed every time it's applied to another instance.
142
142
@@ -147,7 +147,7 @@ To prevent lifecycle methods from being duplicated, we can add **a unique identi
147
147
```ts twoslash
148
148
import { Elysia } from'elysia'
149
149
150
-
const ip =newElysia({ name: 'ip' })
150
+
const ip =newElysia({ name: 'ip' })// [!code ++]
151
151
.derive(
152
152
{ as: 'global' },
153
153
({ server, request }) => ({
@@ -171,11 +171,7 @@ const server = new Elysia()
171
171
172
172
This will prevent the `ip` property from being called multiple times by applying deduplication using a unique name.
173
173
174
-
Once `name` is provided, the instance will become a **singleton**, allowing Elysia to apply plugin deduplication.
175
-
176
-
This allows us to reuse the same instance multiple times without the performance penalty.
177
-
178
-
This forces you to think about the dependencies of each instance, allowing for easily applied migrations or refactoring.
174
+
This allows us to reuse the same instance multiple times without the performance penalty. Forcing you to think about the dependencies of each instance.
179
175
180
176
Learn more about this in [plugin deduplication](/essential/plugin.html#plugin-deduplication).
0 commit comments