From fd14b4a17203bc043b8c9c1f371c2c5ced96eef7 Mon Sep 17 00:00:00 2001 From: Reto Brunner Date: Tue, 22 Nov 2022 03:07:29 +0100 Subject: [PATCH] make getClientConfiguration type safe TS type assertions need to be avoided. The following trivial example demonstrates why ``` type Person = { name: string; isBad: boolean; }; function makePerson(): Person { const p: Person = {name: 'whatever'} as Person p.isBad = false return p // theoretically we are now good, p is a Person } ``` Should the type ever change though, TS will happily trot along ``` type Person = { name: string; isBad: boolean; omgHowCouldYou: number; }; function makePerson(): Person { const p: Person = {name: 'whatever'} as Person p.isBad = true return p // p is *not* a Person, omgHowCouldYou is missing } ``` But we pinky swore to the compiler that p is in fact a Person. In other words, the types are now wrong and you will fail during runtime. --- server/server.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/server/server.ts b/server/server.ts index 76636cb9..ee103997 100644 --- a/server/server.ts +++ b/server/server.ts @@ -421,8 +421,10 @@ function indexRequest(req: Request, res: Response) { throw err; } - const config = getServerConfiguration() as IndexTemplateConfiguration; - config.cacheBust = Helper.getVersionCacheBust(); + const config: IndexTemplateConfiguration = { + ...getServerConfiguration(), + ...{cacheBust: Helper.getVersionCacheBust()}, + }; res.send(_.template(file)(config)); } @@ -901,11 +903,7 @@ function getClientConfiguration(): ClientConfiguration { } function getServerConfiguration(): ServerConfiguration { - const config = _.clone(Config.values) as ServerConfiguration; - - config.stylesheets = packages.getStylesheets(); - - return config; + return {...Config.values, ...{stylesheets: packages.getStylesheets()}}; } function performAuthentication(this: Socket, data) {