From 64395b17b30db991b2855580681fc8c0ad58f9e3 Mon Sep 17 00:00:00 2001 From: Sean O'Donohue Date: Sun, 30 May 2021 12:43:26 -0400 Subject: [PATCH 1/6] Avoid using object type --- src/QuestGoal.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/QuestGoal.ts b/src/QuestGoal.ts index 0364ccdeb..e5a7d1a12 100644 --- a/src/QuestGoal.ts +++ b/src/QuestGoal.ts @@ -9,7 +9,7 @@ export interface IQuestGoalDef { } export interface ISerializedQuestGoal { - state: object; + state: Record; progress: { percent: number; display: string; @@ -30,7 +30,7 @@ export interface IQuestGoalConfig { export class QuestGoal extends EventEmitter { config: IQuestGoalConfig; quest: Quest; - state: object; + state: Record; player: Player; /** * @param {Quest} quest Quest this goal is for @@ -75,7 +75,7 @@ export class QuestGoal extends EventEmitter { }; } - hydrate(state: object) { + hydrate(state: Record) { this.state = state; } } From ecfb6c2a44192ccb7c1defc192a05dcc5527c4d8 Mon Sep 17 00:00:00 2001 From: Sean O'Donohue Date: Sun, 30 May 2021 13:06:09 -0400 Subject: [PATCH 2/6] Use a generic and default to the same type that Quest expects QuestGoal.state to be. --- src/QuestGoal.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/QuestGoal.ts b/src/QuestGoal.ts index e5a7d1a12..ed9083663 100644 --- a/src/QuestGoal.ts +++ b/src/QuestGoal.ts @@ -1,6 +1,6 @@ import { EventEmitter } from 'events'; import { Player } from './Player'; -import { Quest } from './Quest'; +import { ISerializedQuestDef, Quest } from './Quest'; export interface IQuestGoalDef { name: string; @@ -27,10 +27,10 @@ export interface IQuestGoalConfig { * create new quest goals for quests * @extends EventEmitter */ -export class QuestGoal extends EventEmitter { +export class QuestGoal extends EventEmitter { config: IQuestGoalConfig; quest: Quest; - state: Record; + state: TState; player: Player; /** * @param {Quest} quest Quest this goal is for @@ -47,7 +47,7 @@ export class QuestGoal extends EventEmitter { config ); this.quest = quest; - this.state = {}; + this.state = {} as TState; this.player = player; } @@ -69,13 +69,13 @@ export class QuestGoal extends EventEmitter { serialize(): ISerializedQuestGoal { return { - state: this.state, + state: this.state as Record, progress: this.getProgress(), config: this.config, }; } - hydrate(state: Record) { + hydrate(state: TState) { this.state = state; } } From d2c582f85539357852d4abf6493c9bd496554110 Mon Sep 17 00:00:00 2001 From: Sean O'Donohue Date: Sun, 30 May 2021 13:12:08 -0400 Subject: [PATCH 3/6] Turns out some types in Quest were wrong-ish --- src/Quest.ts | 2 +- src/QuestGoal.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Quest.ts b/src/Quest.ts index e5e5e7c2f..26bbe6ca2 100644 --- a/src/Quest.ts +++ b/src/Quest.ts @@ -167,7 +167,7 @@ export class Quest extends EventEmitter { } hydrate() { - (this.state as ISerializedQuestDef[]).forEach((goalState, i: number) => { + (this.state as ISerializedQuestGoal[]).forEach((goalState, i: number) => { this.goals[i].hydrate(goalState.state); }); } diff --git a/src/QuestGoal.ts b/src/QuestGoal.ts index ed9083663..c5419cece 100644 --- a/src/QuestGoal.ts +++ b/src/QuestGoal.ts @@ -27,7 +27,7 @@ export interface IQuestGoalConfig { * create new quest goals for quests * @extends EventEmitter */ -export class QuestGoal extends EventEmitter { +export class QuestGoal> extends EventEmitter { config: IQuestGoalConfig; quest: Quest; state: TState; From 552b88442dce8a32b0b47a940ec289a6f650ef1a Mon Sep 17 00:00:00 2001 From: Sean O'Donohue Date: Sun, 30 May 2021 16:15:45 -0400 Subject: [PATCH 4/6] Make this.state be a Partial of TState so that it can be an empty record type. --- src/QuestGoal.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/QuestGoal.ts b/src/QuestGoal.ts index c5419cece..65162ebbf 100644 --- a/src/QuestGoal.ts +++ b/src/QuestGoal.ts @@ -30,7 +30,7 @@ export interface IQuestGoalConfig { export class QuestGoal> extends EventEmitter { config: IQuestGoalConfig; quest: Quest; - state: TState; + state: Partial; player: Player; /** * @param {Quest} quest Quest this goal is for @@ -47,7 +47,7 @@ export class QuestGoal> extends EventEmitter { config ); this.quest = quest; - this.state = {} as TState; + this.state = {}; this.player = player; } From 152fb88b0f0ab155d4de33a984e23528c5d9b1e4 Mon Sep 17 00:00:00 2001 From: Sean O'Donohue Date: Sun, 30 May 2021 16:18:35 -0400 Subject: [PATCH 5/6] Some cleanup of imports and move jsdoc type to TS --- src/QuestGoal.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/QuestGoal.ts b/src/QuestGoal.ts index 65162ebbf..d4ca67f67 100644 --- a/src/QuestGoal.ts +++ b/src/QuestGoal.ts @@ -1,6 +1,6 @@ import { EventEmitter } from 'events'; import { Player } from './Player'; -import { ISerializedQuestDef, Quest } from './Quest'; +import { Quest } from './Quest'; export interface IQuestGoalDef { name: string; @@ -51,10 +51,7 @@ export class QuestGoal> extends EventEmitter { this.player = player; } - /** - * @return {{ percent: number, display: string}} - */ - getProgress() { + getProgress(): { percent: number, display: string} { return { percent: 0, display: From 0230bc8007e23973d80b0ea6c3f9e38056a5d9d8 Mon Sep 17 00:00:00 2001 From: Sean O'Donohue Date: Tue, 1 Jun 2021 22:50:10 -0400 Subject: [PATCH 6/6] Explicitly type return for getExits so it does not infer 'any' --- src/Room.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Room.ts b/src/Room.ts index 442774890..ef962ac74 100644 --- a/src/Room.ts +++ b/src/Room.ts @@ -243,8 +243,8 @@ export class Room extends GameEntity { * * @return {Array<{ id: string, direction: string, inferred: boolean, room: Room= }>} */ - getExits() { - const exits = JSON.parse(JSON.stringify(this.exits)).map((exit: IExit) => { + getExits(): IExit[] { + const exits: IExit[] = JSON.parse(JSON.stringify(this.exits)).map((exit: IExit) => { exit.inferred = false; return exit; });