From c0809f46e0ad104adb5157ee869babd97427dec4 Mon Sep 17 00:00:00 2001 From: Ethan Niser Date: Mon, 18 Sep 2023 10:29:37 -0500 Subject: [PATCH] make decorates readonly when accessed through context --- src/context.ts | 4 ++-- test/types/index.ts | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/context.ts b/src/context.ts index 9762355f..98762e04 100644 --- a/src/context.ts +++ b/src/context.ts @@ -65,7 +65,7 @@ export type Context< path: string request: Request store: Decorators['store'] - } & Decorators['request'] + } & Readonly > // Use to mimic request before mapping route @@ -92,5 +92,5 @@ export type PreContext< path: string request: Request store: Decorators['store'] - } & Decorators['request'] + } & Readonly > diff --git a/test/types/index.ts b/test/types/index.ts index b67a5abd..7bbb725a 100644 --- a/test/types/index.ts +++ b/test/types/index.ts @@ -854,3 +854,37 @@ app.group( ) ) } + +// ? Decorators are Read-Only in context +{ + const app = new Elysia() + .decorate('a', { + b: 'c', + d: 54 + }) + .decorate('pi', 3.14) + + app.get('/', (ctx) => { + type OnlyDecorators = Omit< + typeof ctx, + | 'params' + | 'request' + | 'body' + | 'headers' + | 'query' + | 'cookie' + | 'set' + | 'path' + | 'r' + | 'store' + > + + expectTypeOf().toEqualTypeOf<{ + readonly a: { + readonly b: 'c' + readonly d: 54 + } + readonly pi: 3.14 + }>() + }) +}