Skip to content

Commit e975888

Browse files
committed
🎉 feat: reorder stuff in key-concept
1 parent 6bc2312 commit e975888

File tree

1 file changed

+40
-44
lines changed

1 file changed

+40
-44
lines changed

docs/key-concept.md

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,44 @@ This forces you to break down your application into small pieces, making it easy
5252

5353
Learn more about this in [plugin](/essential/plugin.html).
5454

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+
new Elysia()
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 = new Elysia()
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+
5593
## Scope
5694
By default, event/life-cycle in each instance is isolated from each other.
5795

@@ -99,44 +137,6 @@ This forces you to think about the scope of each property, preventing you from a
99137

100138
Learn more about this in [scope](/essential/plugin.html#scope).
101139

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-
new Elysia()
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 = new Elysia()
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-
140140
## Dependency
141141
By default, each instance will be re-executed every time it's applied to another instance.
142142

@@ -147,7 +147,7 @@ To prevent lifecycle methods from being duplicated, we can add **a unique identi
147147
```ts twoslash
148148
import { Elysia } from 'elysia'
149149

150-
const ip = new Elysia({ name: 'ip' })
150+
const ip = new Elysia({ name: 'ip' }) // [!code ++]
151151
.derive(
152152
{ as: 'global' },
153153
({ server, request }) => ({
@@ -171,11 +171,7 @@ const server = new Elysia()
171171

172172
This will prevent the `ip` property from being called multiple times by applying deduplication using a unique name.
173173

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.
179175

180176
Learn more about this in [plugin deduplication](/essential/plugin.html#plugin-deduplication).
181177

0 commit comments

Comments
 (0)