From 78ca3713a19bfb600f1e43b05ecbb5bd03cdd219 Mon Sep 17 00:00:00 2001 From: thepaperpilot Date: Tue, 17 Aug 2021 18:39:11 -0500 Subject: [PATCH] Added type predicate to setDefault --- src/game/layers.ts | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/game/layers.ts b/src/game/layers.ts index 2ce627a..c4c9783 100644 --- a/src/game/layers.ts +++ b/src/game/layers.ts @@ -96,7 +96,7 @@ export function addLayer(layer: RawLayer, player?: Partial): void { RawGridFeatures, Upgrade>, GridFeatures, Upgrade - >(layer.id, layer.upgrades!); + >(layer.id, layer.upgrades); setRowCol(layer.upgrades); for (const id in layer.upgrades.data) { layer.upgrades.data[id].bought = function() { @@ -193,7 +193,7 @@ export function addLayer(layer: RawLayer, player?: Partial): void { RawGridFeatures, Achievement>, GridFeatures, Achievement - >(layer.id, layer.achievements!); + >(layer.id, layer.achievements); setRowCol(layer.achievements); for (const id in layer.achievements.data) { layer.achievements.data[id].earned = function() { @@ -461,8 +461,8 @@ export function addLayer(layer: RawLayer, player?: Partial): void { ); }; setDefault(player, "subtabs", {}); - setDefault(player.subtabs!, layer.id, {}); - setDefault(player.subtabs![layer.id], "mainTabs", Object.keys(layer.subtabs)[0]); + setDefault(player.subtabs, layer.id, {}); + setDefault(player.subtabs[layer.id], "mainTabs", Object.keys(layer.subtabs)[0]); for (const id in layer.subtabs) { layer.subtabs[id].active = function() { return playerProxy.subtabs[this.layer].mainTabs === this.id; @@ -471,7 +471,7 @@ export function addLayer(layer: RawLayer, player?: Partial): void { } if (layer.microtabs) { setDefault(player, "subtabs", {}); - setDefault(player.subtabs!, layer.id, {}); + setDefault(player.subtabs, layer.id, {}); for (const family in layer.microtabs) { if (Object.keys(layer.microtabs[family]).length === 0) { console.warn( @@ -497,7 +497,7 @@ export function addLayer(layer: RawLayer, player?: Partial): void { return firstUnlocked != undefined ? this[firstUnlocked] : undefined; }; setDefault( - player.subtabs![layer.id], + player.subtabs[layer.id], family, Object.keys(layer.microtabs[family]).find(tab => tab !== "activeMicrotab")! ); @@ -594,7 +594,12 @@ function setupFeatures, R extends Features, S ext } } -function setDefault(object: T, key: K, value: T[K], forceCached?: boolean) { +function setDefault( + object: T, + key: K, + value: T[K], + forceCached?: boolean +): asserts object is Exclude & Required> { if (object[key] == undefined && value != undefined) { object[key] = value; }