vibes-plug 1.0.0 → 2.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/publish.yml +20 -0
- package/AGENTS.md +66 -0
- package/BLUEPRINT.md +152 -60
- package/CHANGELOG.md +50 -0
- package/README.md +346 -194
- package/package.json +25 -25
- package/plugin.json +1 -1
- package/skills/ai-cost-token-optimizer/SKILL.md +52 -0
- package/skills/ai-llm-integration-expert/SKILL.md +180 -162
- package/skills/api-design-expert/SKILL.md +314 -310
- package/skills/app-analyzer-optimizer/SKILL.md +193 -189
- package/skills/apple-ecosystem-expert/SKILL.md +142 -0
- package/skills/async-queue-temporal-expert/SKILL.md +54 -0
- package/skills/authentication-identity-expert/SKILL.md +251 -20
- package/skills/auto-doc-updater/SKILL.md +214 -204
- package/skills/autonomous-chaos-monkey/SKILL.md +63 -0
- package/skills/autonomous-red-teamer/SKILL.md +59 -0
- package/skills/autonomous-swarm-director/SKILL.md +69 -0
- package/skills/autonomous-tdd-debugger/SKILL.md +65 -0
- package/skills/bootstrap-to-modern/SKILL.md +90 -86
- package/skills/brainstorming/SKILL.md +373 -353
- package/skills/browser-automation-expert/SKILL.md +46 -0
- package/skills/ci-cd-devops-architect/SKILL.md +72 -45
- package/skills/cloud-hosting-expert/SKILL.md +244 -244
- package/skills/coderabbit/SKILL.md +192 -192
- package/skills/cron-scheduler-expert/SKILL.md +298 -0
- package/skills/data-telemetry-expert/SKILL.md +213 -213
- package/skills/database-orm-expert/SKILL.md +294 -294
- package/skills/dependency-upgrade-migrator/SKILL.md +295 -0
- package/skills/design-system-architect/SKILL.md +27 -10
- package/skills/doku-mcp-server/SKILL.md +251 -0
- package/skills/doku-payment-gateway/SKILL.md +227 -0
- package/skills/e2e-testing-expert/SKILL.md +315 -315
- package/skills/edge-serverless-db-expert/SKILL.md +43 -0
- package/skills/email-notification-expert/SKILL.md +362 -0
- package/skills/error-resilience-expert/SKILL.md +480 -0
- package/skills/event-driven-architect/SKILL.md +81 -81
- package/skills/feature-flag-analytics-expert/SKILL.md +46 -0
- package/skills/file-upload-media-expert/SKILL.md +431 -0
- package/skills/form-validation-expert/SKILL.md +401 -0
- package/skills/fullstack-expert/SKILL.md +202 -202
- package/skills/fullstack-expert/references/api_design_guide.md +466 -466
- package/skills/fullstack-expert/references/multi_language_backend.md +528 -528
- package/skills/fullstack-expert/scripts/api_contract_validator.py +253 -253
- package/skills/fullstack-expert/scripts/architecture_analyzer.py +326 -326
- package/skills/gemini-agent-booster/SKILL.md +135 -135
- package/skills/global-a11y-i18n-expert/SKILL.md +81 -81
- package/skills/glsl-shader-expert/SKILL.md +101 -0
- package/skills/go-programming-expert/SKILL.md +295 -295
- package/skills/graphql-apollo-expert/SKILL.md +108 -0
- package/skills/hig/SKILL.md +188 -188
- package/skills/hyper-context-synthesizer/SKILL.md +55 -0
- package/skills/js-backend-expert/SKILL.md +34 -9
- package/skills/legacy-code-translator/SKILL.md +65 -0
- package/skills/llm-cost-arbitrage-router/SKILL.md +59 -0
- package/skills/logging-error-tracking-expert/SKILL.md +338 -0
- package/skills/mcp-client-orchestrator/SKILL.md +70 -0
- package/skills/mcp-server-architect/SKILL.md +194 -194
- package/skills/micro-frontend-architect/SKILL.md +106 -0
- package/skills/mobile-expo-expert/SKILL.md +186 -186
- package/skills/mobile-push-notification-expert/SKILL.md +51 -0
- package/skills/monday-design-aesthetic/SKILL.md +67 -67
- package/skills/monorepo-architect/SKILL.md +227 -227
- package/skills/mpa-orchestrator/SKILL.md +101 -101
- package/skills/multi-agent-orchestration/SKILL.md +234 -234
- package/skills/multiple-entry-points/SKILL.md +55 -55
- package/skills/mvc-expert/SKILL.md +231 -231
- package/skills/payment-gateway-expert/SKILL.md +45 -45
- package/skills/performance-web-vitals/SKILL.md +332 -332
- package/skills/post-quantum-crypto-migrator/SKILL.md +57 -0
- package/skills/prd-architect/SKILL.md +201 -191
- package/skills/proactive-background-watcher/SKILL.md +62 -0
- package/skills/production-ready-hardener/PRODUCTION_READINESS_REPORT.md +67 -0
- package/skills/production-ready-hardener/SKILL.md +173 -186
- package/skills/production-ready-hardener/references/production_checklist.md +161 -161
- package/skills/production-ready-hardener/scripts/production_readiness_scanner.py +881 -875
- package/skills/project-context-mapper/SKILL.md +79 -0
- package/skills/python-programming-expert/SKILL.md +263 -132
- package/skills/rate-limit-abuse-prevention/SKILL.md +371 -0
- package/skills/realtime-collaboration-expert/SKILL.md +45 -45
- package/skills/rust-programming-expert/SKILL.md +235 -235
- package/skills/saas-billing/SKILL.md +377 -377
- package/skills/saas-multi-tenant/SKILL.md +251 -237
- package/skills/saas-mvp-launcher/SKILL.md +10 -0
- package/skills/saas-transformer/SKILL.md +187 -144
- package/skills/saas-transformer/references/billing_integration_guide.md +401 -401
- package/skills/saas-transformer/references/feature_gating_patterns.md +137 -137
- package/skills/saas-transformer/references/saas_transformation_checklist.md +121 -121
- package/skills/saas-transformer/scripts/saas_transformation_scanner.py +39 -29
- package/skills/scalability-clean-code/SKILL.md +229 -229
- package/skills/self-evolving-memory-graph/SKILL.md +75 -0
- package/skills/self-healing-cloud-orchestrator/SKILL.md +57 -0
- package/skills/senior-frontend/SKILL.md +161 -161
- package/skills/senior-fullstack/SKILL.md +167 -167
- package/skills/seo/SKILL.md +235 -225
- package/skills/seo-geo/SKILL.md +188 -188
- package/skills/session-context-loader/SKILL.md +77 -0
- package/skills/session-handoff-resume/SKILL.md +158 -158
- package/skills/skill_baru/SKILL.md +172 -147
- package/skills/spa-orchestrator/SKILL.md +288 -288
- package/skills/state-management-expert/SKILL.md +272 -272
- package/skills/supabase-security-expert/SKILL.md +243 -243
- package/skills/tailwind-expert/SKILL.md +188 -188
- package/skills/tanstack-query-expert/SKILL.md +199 -199
- package/skills/token-saver/SKILL.md +119 -111
- package/skills/typescript-expert/SKILL.md +324 -279
- package/skills/ui-components-expert/SKILL.md +263 -46
- package/skills/ui-ux-pro-max/SKILL.md +202 -201
- package/skills/ui-ux-pro-max/scripts/__pycache__/core.cpython-310.pyc +0 -0
- package/skills/ui-ux-pro-max/scripts/__pycache__/design_system.cpython-310.pyc +0 -0
- package/skills/ui_ux_expert/SKILL.md +17 -6
- package/skills/vector-db-rag-expert/SKILL.md +52 -0
- package/skills/vibe-code-gardener/SKILL.md +181 -173
- package/skills/visual-qa-vision-agent/SKILL.md +65 -0
- package/skills/vue-frontend-expert/SKILL.md +126 -0
- package/skills/web-3d-graphics-expert/SKILL.md +131 -0
- package/skills/web-game-engine-expert/SKILL.md +96 -0
- package/skills/web-scraper/SKILL.md +207 -205
- package/skills/website-design-cloner/SKILL.md +174 -0
- package/skills/webxr-ar-vr-expert/SKILL.md +117 -0
- package/skills/zero-to-prod-orchestrator/SKILL.md +206 -180
- package/skills/zero-trust-secret-vault/SKILL.md +40 -0
- package/vibes-swarm-demo.gif +0 -0
|
@@ -1,279 +1,324 @@
|
|
|
1
|
-
---
|
|
2
|
-
name: typescript-expert
|
|
3
|
-
description: "Expert guide for TypeScript 5.
|
|
4
|
-
author: "Roedy Rustam"
|
|
5
|
-
---
|
|
6
|
-
|
|
7
|
-
# TypeScript Expert (TypeScript 5.
|
|
8
|
-
|
|
9
|
-
[English](#english) | [Bahasa Indonesia](#bahasa-indonesia)
|
|
10
|
-
|
|
11
|
-
---
|
|
12
|
-
|
|
13
|
-
<a name="english"></a>
|
|
14
|
-
## English
|
|
15
|
-
|
|
16
|
-
### Description
|
|
17
|
-
Expert-level TypeScript development covering the advanced type system, strict mode enforcement, generic programming, utility types, branded types, and type-safe patterns for production applications. Targets **TypeScript 5.
|
|
18
|
-
|
|
19
|
-
### Trigger Conditions
|
|
20
|
-
- Writing TypeScript with advanced generic constraints.
|
|
21
|
-
- Enforcing strict type safety in existing codebases.
|
|
22
|
-
- Designing type-safe API contracts (REST, tRPC, Zod schemas).
|
|
23
|
-
- Implementing branded types for domain modeling.
|
|
24
|
-
- Resolving complex type errors or `any` pollution.
|
|
25
|
-
- Setting up `tsconfig.json` for strict projects.
|
|
26
|
-
- Writing TypeScript utility types or type helpers.
|
|
27
|
-
|
|
28
|
-
---
|
|
29
|
-
|
|
30
|
-
### TypeScript 5.x — Key Features
|
|
31
|
-
|
|
32
|
-
#### `using` Declarations (Explicit Resource Management, TS 5.2)
|
|
33
|
-
```typescript
|
|
34
|
-
// Automatically calls [Symbol.dispose] on scope exit
|
|
35
|
-
function processFile(path: string) {
|
|
36
|
-
using handle = openFile(path); // disposed when function exits
|
|
37
|
-
handle.write('data');
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// Async version with [Symbol.asyncDispose]
|
|
41
|
-
async function processDatabase() {
|
|
42
|
-
await using conn = await getConnection();
|
|
43
|
-
await conn.query('SELECT 1');
|
|
44
|
-
} // conn.close() called automatically
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
#### `NoInfer<T>` Utility Type (TS 5.4)
|
|
48
|
-
```typescript
|
|
49
|
-
// Prevents unintended type widening in generic inference
|
|
50
|
-
function createState<T>(initial: T, fallback: NoInfer<T>): T {
|
|
51
|
-
return initial ?? fallback;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// TS now errors if fallback type doesn't match initial
|
|
55
|
-
createState('hello', 42); // Error: Argument of type 'number' is not assignable to type 'string'
|
|
56
|
-
```
|
|
57
|
-
|
|
58
|
-
#### `const` Type Parameters (TS 5.0)
|
|
59
|
-
```typescript
|
|
60
|
-
// Infer literal types from generic arguments
|
|
61
|
-
function identity<const T>(value: T): T { return value; }
|
|
62
|
-
|
|
63
|
-
const a = identity(['a', 'b', 'c']); // type: readonly ["a", "b", "c"]
|
|
64
|
-
const b = identity({ x: 10 }); // type: { readonly x: 10 }
|
|
65
|
-
```
|
|
66
|
-
|
|
67
|
-
---
|
|
68
|
-
|
|
69
|
-
###
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
//
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
type
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
```
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
<
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
-
|
|
278
|
-
-
|
|
279
|
-
|
|
1
|
+
---
|
|
2
|
+
name: typescript-expert
|
|
3
|
+
description: "Expert guide for TypeScript 5.5+ advanced type system, strict mode, generics, utility types, branded types, inferred type predicates, isolated declarations, and type-safe architectural patterns / Panduan ahli untuk sistem tipe TypeScript 5.5+, mode strict, generics, utility types, branded types, inferred type predicates, isolated declarations, dan pola arsitektur type-safe."
|
|
4
|
+
author: "Roedy Rustam"
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
# TypeScript Expert (TypeScript 5.5+ Edition)
|
|
8
|
+
|
|
9
|
+
[English](#english) | [Bahasa Indonesia](#bahasa-indonesia)
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
<a name="english"></a>
|
|
14
|
+
## English
|
|
15
|
+
|
|
16
|
+
### Description
|
|
17
|
+
Expert-level TypeScript development covering the advanced type system, strict mode enforcement, generic programming, utility types, branded types, and type-safe patterns for production applications. Targets **TypeScript 5.5+** features including inferred type predicates, isolated declarations, `NoInfer`, `using` declarations, variadic tuple improvements, and `const` type parameters.
|
|
18
|
+
|
|
19
|
+
### Trigger Conditions
|
|
20
|
+
- Writing TypeScript with advanced generic constraints.
|
|
21
|
+
- Enforcing strict type safety in existing codebases.
|
|
22
|
+
- Designing type-safe API contracts (REST, tRPC, Zod schemas).
|
|
23
|
+
- Implementing branded types for domain modeling.
|
|
24
|
+
- Resolving complex type errors or `any` pollution.
|
|
25
|
+
- Setting up `tsconfig.json` for strict projects.
|
|
26
|
+
- Writing TypeScript utility types or type helpers.
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
### TypeScript 5.x — Key Features
|
|
31
|
+
|
|
32
|
+
#### `using` Declarations (Explicit Resource Management, TS 5.2)
|
|
33
|
+
```typescript
|
|
34
|
+
// Automatically calls [Symbol.dispose] on scope exit
|
|
35
|
+
function processFile(path: string) {
|
|
36
|
+
using handle = openFile(path); // disposed when function exits
|
|
37
|
+
handle.write('data');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Async version with [Symbol.asyncDispose]
|
|
41
|
+
async function processDatabase() {
|
|
42
|
+
await using conn = await getConnection();
|
|
43
|
+
await conn.query('SELECT 1');
|
|
44
|
+
} // conn.close() called automatically
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
#### `NoInfer<T>` Utility Type (TS 5.4)
|
|
48
|
+
```typescript
|
|
49
|
+
// Prevents unintended type widening in generic inference
|
|
50
|
+
function createState<T>(initial: T, fallback: NoInfer<T>): T {
|
|
51
|
+
return initial ?? fallback;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// TS now errors if fallback type doesn't match initial
|
|
55
|
+
createState('hello', 42); // Error: Argument of type 'number' is not assignable to type 'string'
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
#### `const` Type Parameters (TS 5.0)
|
|
59
|
+
```typescript
|
|
60
|
+
// Infer literal types from generic arguments
|
|
61
|
+
function identity<const T>(value: T): T { return value; }
|
|
62
|
+
|
|
63
|
+
const a = identity(['a', 'b', 'c']); // type: readonly ["a", "b", "c"]
|
|
64
|
+
const b = identity({ x: 10 }); // type: { readonly x: 10 }
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
---
|
|
68
|
+
|
|
69
|
+
### TypeScript 5.5 / 5.6 — New Features
|
|
70
|
+
|
|
71
|
+
#### Inferred Type Predicates (TS 5.5)
|
|
72
|
+
```typescript
|
|
73
|
+
// TypeScript now infers type predicates from return statements automatically
|
|
74
|
+
const nums = [1, null, 2, undefined, 3].filter((x) => x !== null);
|
|
75
|
+
// nums is now inferred as number[] — no manual type assertion needed!
|
|
76
|
+
|
|
77
|
+
// Before TS 5.5 you needed:
|
|
78
|
+
const nums = [1, null, 2].filter((x): x is number => x !== null);
|
|
79
|
+
|
|
80
|
+
// Works with any refinement pattern:
|
|
81
|
+
function isString(x: unknown) {
|
|
82
|
+
return typeof x === 'string'; // TS 5.5 infers: (x: unknown) => x is string
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
#### Isolated Declarations (TS 5.5)
|
|
87
|
+
```typescript
|
|
88
|
+
// New tsconfig option: "isolatedDeclarations": true
|
|
89
|
+
// Forces explicit return types on all exported functions — enables
|
|
90
|
+
// parallel .d.ts generation (massively speeds up monorepo builds)
|
|
91
|
+
export function add(a: number, b: number): number { // explicit return type required
|
|
92
|
+
return a + b;
|
|
93
|
+
}
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
#### Iterator Helper Methods (TS 5.6 — ES2025)
|
|
97
|
+
```typescript
|
|
98
|
+
// Native iterator methods now fully typed
|
|
99
|
+
const result = [1, 2, 3, 4, 5]
|
|
100
|
+
.values() // IteratorObject
|
|
101
|
+
.filter(x => x % 2 === 0) // 2, 4
|
|
102
|
+
.map(x => x * 10) // 20, 40
|
|
103
|
+
.toArray(); // [20, 40]
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
---
|
|
107
|
+
|
|
108
|
+
### Strict Mode Configuration
|
|
109
|
+
|
|
110
|
+
```json
|
|
111
|
+
// tsconfig.json — recommended strict config for Next.js / monorepo projects
|
|
112
|
+
{
|
|
113
|
+
"compilerOptions": {
|
|
114
|
+
"target": "ES2022",
|
|
115
|
+
"lib": ["ES2022", "DOM", "DOM.Iterable"],
|
|
116
|
+
"module": "Preserve",
|
|
117
|
+
"moduleResolution": "Bundler",
|
|
118
|
+
"strict": true,
|
|
119
|
+
"noUncheckedIndexedAccess": true,
|
|
120
|
+
"exactOptionalPropertyTypes": true,
|
|
121
|
+
"noImplicitReturns": true,
|
|
122
|
+
"noFallthroughCasesInSwitch": true,
|
|
123
|
+
"noImplicitOverride": true,
|
|
124
|
+
"isolatedModules": true,
|
|
125
|
+
"isolatedDeclarations": true,
|
|
126
|
+
"verbatimModuleSyntax": true,
|
|
127
|
+
"forceConsistentCasingInFileNames": true,
|
|
128
|
+
"skipLibCheck": true
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
> **Note**: Use `"moduleResolution": "Bundler"` with `"module": "Preserve"` for Vite, Next.js, and other bundler-based projects. Use `"NodeNext"` for Node.js/Bun/Deno runtimes.
|
|
134
|
+
|
|
135
|
+
---
|
|
136
|
+
|
|
137
|
+
### Advanced Type Patterns
|
|
138
|
+
|
|
139
|
+
#### Branded Types for Domain Modeling
|
|
140
|
+
```typescript
|
|
141
|
+
// Prevent mixing semantically different primitives
|
|
142
|
+
type Brand<T, B extends string> = T & { readonly __brand: B };
|
|
143
|
+
|
|
144
|
+
type UserId = Brand<string, 'UserId'>;
|
|
145
|
+
type PostId = Brand<string, 'PostId'>;
|
|
146
|
+
type Email = Brand<string, 'Email'>;
|
|
147
|
+
|
|
148
|
+
// Constructor functions with validation
|
|
149
|
+
function createUserId(id: string): UserId {
|
|
150
|
+
if (!id.startsWith('user_')) throw new Error('Invalid user ID format');
|
|
151
|
+
return id as UserId;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function getUser(id: UserId): Promise<User> { /* ... */ }
|
|
155
|
+
|
|
156
|
+
const postId = 'post_abc' as PostId;
|
|
157
|
+
getUser(postId); // Compile error: PostId is not assignable to UserId
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
#### Discriminated Unions for State Machines
|
|
161
|
+
```typescript
|
|
162
|
+
type ApiState<T> =
|
|
163
|
+
| { status: 'idle' }
|
|
164
|
+
| { status: 'loading' }
|
|
165
|
+
| { status: 'success'; data: T }
|
|
166
|
+
| { status: 'error'; error: Error };
|
|
167
|
+
|
|
168
|
+
function render<T>(state: ApiState<T>) {
|
|
169
|
+
switch (state.status) {
|
|
170
|
+
case 'idle': return 'Idle';
|
|
171
|
+
case 'loading': return 'Loading...';
|
|
172
|
+
case 'success': return `Data: ${JSON.stringify(state.data)}`;
|
|
173
|
+
case 'error': return `Error: ${state.error.message}`;
|
|
174
|
+
// TypeScript enforces exhaustive matching
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
#### Template Literal Types
|
|
180
|
+
```typescript
|
|
181
|
+
type EventName = 'click' | 'focus' | 'blur';
|
|
182
|
+
type HandlerName = `on${Capitalize<EventName>}`;
|
|
183
|
+
// Result: 'onClick' | 'onFocus' | 'onBlur'
|
|
184
|
+
|
|
185
|
+
type DeepReadonly<T> = {
|
|
186
|
+
readonly [K in keyof T]: T[K] extends object ? DeepReadonly<T[K]> : T[K];
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
type Paths<T, Prefix extends string = ''> = {
|
|
190
|
+
[K in keyof T & string]: T[K] extends object
|
|
191
|
+
? Paths<T[K], `${Prefix}${K}.`>
|
|
192
|
+
: `${Prefix}${K}`;
|
|
193
|
+
}[keyof T & string];
|
|
194
|
+
|
|
195
|
+
// Paths<{ user: { name: string; age: number } }> = "user.name" | "user.age"
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
#### Conditional Types and `infer`
|
|
199
|
+
```typescript
|
|
200
|
+
type UnwrapPromise<T> = T extends Promise<infer U> ? U : T;
|
|
201
|
+
type ArrayElement<T> = T extends (infer U)[] ? U : never;
|
|
202
|
+
type ReturnType<T> = T extends (...args: any[]) => infer R ? R : never;
|
|
203
|
+
|
|
204
|
+
// Extract function parameter types
|
|
205
|
+
type Parameters<T extends (...args: any) => any> =
|
|
206
|
+
T extends (...args: infer P) => any ? P : never;
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
---
|
|
210
|
+
|
|
211
|
+
### Type-Safe Patterns
|
|
212
|
+
|
|
213
|
+
#### Zod Schema + TypeScript Integration
|
|
214
|
+
```typescript
|
|
215
|
+
import { z } from 'zod';
|
|
216
|
+
|
|
217
|
+
const UserSchema = z.object({
|
|
218
|
+
id: z.string().cuid(),
|
|
219
|
+
email: z.string().email(),
|
|
220
|
+
role: z.enum(['USER', 'ADMIN']),
|
|
221
|
+
createdAt: z.coerce.date(),
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
type User = z.infer<typeof UserSchema>; // Derive type from schema
|
|
225
|
+
|
|
226
|
+
// Type-safe parsing with error handling
|
|
227
|
+
function parseUser(data: unknown): User {
|
|
228
|
+
return UserSchema.parse(data); // throws ZodError on failure
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const safeResult = UserSchema.safeParse(data);
|
|
232
|
+
if (safeResult.success) {
|
|
233
|
+
console.log(safeResult.data.email); // fully typed
|
|
234
|
+
}
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
#### Type-Safe Environment Variables
|
|
238
|
+
```typescript
|
|
239
|
+
// env.ts — validate env at startup
|
|
240
|
+
import { z } from 'zod';
|
|
241
|
+
|
|
242
|
+
const envSchema = z.object({
|
|
243
|
+
DATABASE_URL: z.string().url(),
|
|
244
|
+
NEXTAUTH_SECRET: z.string().min(32),
|
|
245
|
+
NODE_ENV: z.enum(['development', 'test', 'production']),
|
|
246
|
+
PORT: z.coerce.number().default(3000),
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
export const env = envSchema.parse(process.env);
|
|
250
|
+
// env.PORT is now type `number`, not `string | undefined`
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
#### Generic Repository Pattern
|
|
254
|
+
```typescript
|
|
255
|
+
interface Repository<T, TId> {
|
|
256
|
+
findById(id: TId): Promise<T | null>;
|
|
257
|
+
findMany(filter?: Partial<T>): Promise<T[]>;
|
|
258
|
+
create(data: Omit<T, 'id' | 'createdAt' | 'updatedAt'>): Promise<T>;
|
|
259
|
+
update(id: TId, data: Partial<Omit<T, 'id'>>): Promise<T>;
|
|
260
|
+
delete(id: TId): Promise<void>;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
class UserRepository implements Repository<User, UserId> {
|
|
264
|
+
async findById(id: UserId) { /* ... */ }
|
|
265
|
+
// TypeScript enforces all interface methods are implemented
|
|
266
|
+
}
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
---
|
|
270
|
+
|
|
271
|
+
### Common Pitfalls to Avoid
|
|
272
|
+
|
|
273
|
+
| Anti-Pattern | Problem | Solution |
|
|
274
|
+
|---|---|---|
|
|
275
|
+
| `as any` | Disables type checking | Use `unknown` + narrowing or Zod |
|
|
276
|
+
| `as Type` (unsafe cast) | Bypasses structural checking | Use type guards or `satisfies` |
|
|
277
|
+
| `// @ts-ignore` | Silences real errors | Fix the root type issue |
|
|
278
|
+
| `!` non-null assertion | Runtime errors if null | Use optional chaining + nullish coalescing |
|
|
279
|
+
| `Object` / `{}` type | Accepts anything non-null | Use specific types or `Record<string, unknown>` |
|
|
280
|
+
| Implicit `any` in callbacks | Breaks type inference | Always type function parameters |
|
|
281
|
+
|
|
282
|
+
---
|
|
283
|
+
|
|
284
|
+
### The `satisfies` Operator (TS 4.9+)
|
|
285
|
+
```typescript
|
|
286
|
+
// Validates against a type without widening the inferred type
|
|
287
|
+
const config = {
|
|
288
|
+
port: 3000,
|
|
289
|
+
host: 'localhost',
|
|
290
|
+
debug: true,
|
|
291
|
+
} satisfies Record<string, string | number | boolean>;
|
|
292
|
+
|
|
293
|
+
// config.port is still inferred as `3000` (literal), not `number`
|
|
294
|
+
config.port.toFixed(2); // Works! Literal type preserved.
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
---
|
|
298
|
+
|
|
299
|
+
<a name="bahasa-indonesia"></a>
|
|
300
|
+
## Bahasa Indonesia
|
|
301
|
+
|
|
302
|
+
### Deskripsi
|
|
303
|
+
Panduan TypeScript level ahli mencakup sistem tipe tingkat lanjut, penerapan strict mode, pemrograman generik, utility types, branded types, dan pola type-safe untuk aplikasi produksi. Menargetkan fitur **TypeScript 5.5+** termasuk inferred type predicates, isolated declarations, `NoInfer`, deklarasi `using`, peningkatan variadic tuple, dan parameter tipe `const`.
|
|
304
|
+
|
|
305
|
+
### Kondisi Pemicu
|
|
306
|
+
- Menulis TypeScript dengan generic constraints tingkat lanjut.
|
|
307
|
+
- Menerapkan type safety ketat di codebase yang ada.
|
|
308
|
+
- Merancang kontrak API type-safe (REST, tRPC, Zod schema).
|
|
309
|
+
- Mengimplementasikan branded types untuk pemodelan domain.
|
|
310
|
+
- Menyelesaikan type error kompleks atau polusi `any`.
|
|
311
|
+
- Menyiapkan `tsconfig.json` untuk proyek strict dengan `isolatedDeclarations`.
|
|
312
|
+
- Menulis utility types atau type helpers TypeScript.
|
|
313
|
+
|
|
314
|
+
### Panduan Singkat
|
|
315
|
+
|
|
316
|
+
- **Aktifkan strict mode**: Selalu gunakan `"strict": true` ditambah `noUncheckedIndexedAccess`, `exactOptionalPropertyTypes`, dan `isolatedDeclarations`.
|
|
317
|
+
- **Inferred Type Predicates** (TS 5.5): Filter array tanpa type assertion manual — TypeScript inferensikan sendiri.
|
|
318
|
+
- **Isolated Declarations** (TS 5.5): Aktifkan untuk mempercepat build monorepo via parallel `.d.ts` generation.
|
|
319
|
+
- **Branded Types**: Cegah pencampuran primitif yang berbeda secara semantis (UserId vs PostId).
|
|
320
|
+
- **Discriminated Union**: Gunakan untuk state machine dan variant data yang terbatas.
|
|
321
|
+
- **Zod**: Validasi data eksternal dan turunkan tipe TypeScript dari schema Zod.
|
|
322
|
+
- **Hindari `as any`**: Gunakan `unknown` dengan narrowing atau Zod untuk data yang tidak diketahui tipenya.
|
|
323
|
+
- **`satisfies` operator**: Validasi objek terhadap tipe tanpa melebarkan tipe yang diinferensi.
|
|
324
|
+
- **`moduleResolution: Bundler`**: Gunakan untuk proyek Next.js/Vite; `NodeNext` untuk Node.js/Bun backend.
|