From 73069f1de352cc0e7ffd98f8d1318acde8197003 Mon Sep 17 00:00:00 2001
From: Emile Fokkema <emile.fokkema@navara.nl>
Date: Sat, 16 Nov 2024 22:54:43 +0100
Subject: [PATCH] fix: type of Rule constructor and Rule's name

---
 test/rule.test.mjs    | 5 +++++
 test/types.test-d.mts | 5 +++++
 types/index.d.ts      | 4 ++--
 3 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/test/rule.test.mjs b/test/rule.test.mjs
index 05c26ae..732f1e1 100644
--- a/test/rule.test.mjs
+++ b/test/rule.test.mjs
@@ -12,6 +12,11 @@ describe("Rule", () => {
   });
 
   describe("constructor()", () => {
+    it("can be initialized without anything", () => {
+      const rule = new Rule();
+      expect(rule.name).toBeUndefined();
+    })
+
     it("can be initialized with priority, conditions, event, and name", () => {
       const condition = {
         all: [Object.assign({}, conditionBase)],
diff --git a/test/types.test-d.mts b/test/types.test-d.mts
index 69bb309..b05c671 100644
--- a/test/types.test-d.mts
+++ b/test/types.test-d.mts
@@ -87,6 +87,11 @@ describe("type tests", () => {
     const rule = new Rule(ruleProps);
     const ruleFromString: Rule = new Rule(JSON.stringify(ruleProps));
 
+    it("name of rule is possibly undefined", () => {
+      const ruleWithoutInit = new Rule();
+      expectTypeOf<string | undefined>(ruleWithoutInit.name);
+    })
+
     it("returns the engine when adding a rule", () => {
       expectTypeOf<Engine>(engine.addRule(rule));
     });
diff --git a/types/index.d.ts b/types/index.d.ts
index 596cedc..591c4d7 100644
--- a/types/index.d.ts
+++ b/types/index.d.ts
@@ -189,8 +189,8 @@ export interface RuleResult {
 }
 
 export class Rule implements RuleProperties {
-  constructor(ruleProps: RuleProperties | string);
-  name: string;
+  constructor(ruleProps?: RuleProperties | string);
+  name?: string;
   conditions: TopLevelCondition;
   event: Event;
   priority: number;