Skip to content

Commit b91e550

Browse files
ggazzoclaude
andcommitted
docs: add pitfalls for rest-typings removal and as const usage
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
1 parent 2c8cd35 commit b91e550

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

docs/api-endpoint-migration.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,36 @@ Source: `apps/meteor/app/api/server/v1/invites.ts`
569569
4. **Augment `Endpoints`**: Use `declare module '@rocket.chat/rest-typings'` to merge the extracted types into the global `Endpoints` interface. This is what makes `useEndpoint('listInvites')` and similar SDK calls type-safe.
570570
5. **Import `ExtractRoutesFromAPI`** from `'../ApiClass'` (relative to the endpoint file in `v1/`).
571571

572+
### Keep types in `rest-typings` (do NOT remove them)
573+
574+
The `declare module` augmentation via `ExtractRoutesFromAPI` only works within the `apps/meteor` compilation unit. External packages (`ddp-client`, `rest-client`, etc.) compile independently and **do not see** the augmented types — they only see the types exported from `@rocket.chat/rest-typings`.
575+
576+
**When migrating an endpoint, keep its type definition in `rest-typings` unchanged.** The augmentation adds response schema types on top of the existing definition. Removing the type from `rest-typings` will break external package consumers.
577+
578+
This duplication is temporary — see `docs/api-definitions-package-plan.md` for the planned consolidation.
579+
580+
### Use `as const` on options variables
581+
582+
When endpoint options are stored in a separate variable (required for sharing between action factories), add `as const` so that `authRequired: true` is inferred as the literal `true`, not `boolean`. This matters because `TypedThis` uses a conditional type:
583+
584+
```typescript
585+
userId: TOptions['authRequired'] extends true ? string : string | undefined;
586+
```
587+
588+
Without `as const`, `authRequired` is `boolean`, and `userId` becomes `string | undefined` — forcing unnecessary guards in the action body.
589+
590+
```typescript
591+
// Correct — userId is string, bodyParams is typed
592+
const myEndpointProps = {
593+
authRequired: true,
594+
body: isMyProps,
595+
response: { ... },
596+
} as const;
597+
598+
// Inline options don't need as const — TypeScript infers literals from the generic context
599+
API.v1.post('myEndpoint', { authRequired: true, body: isMyProps, response: { ... } }, async function action() { ... });
600+
```
601+
572602
### What augmentation enables
573603

574604
Once the `Endpoints` interface is augmented, the entire stack benefits:

0 commit comments

Comments
 (0)