Skip to content

Commit

Permalink
Merge pull request #1299 from marceliwac/issue/1297
Browse files Browse the repository at this point in the history
fix: Update default type for ServiceSchema generic to Service<S>.
  • Loading branch information
icebob authored Oct 17, 2024
2 parents 7c4846d + efb777c commit 3375833
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 10 deletions.
14 changes: 6 additions & 8 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -720,12 +720,10 @@ declare namespace Moleculer {
version?: string | number;
}

type ServiceSyncLifecycleHandler<S = ServiceSettingSchema, T = Service<S>> = (this: T) => void;
type ServiceAsyncLifecycleHandler<S = ServiceSettingSchema, T = Service<S>> = (
this: T
) => void | Promise<void>;
type ServiceSyncLifecycleHandler<T> = (this: T) => void;
type ServiceAsyncLifecycleHandler<T> = (this: T) => void | Promise<void>;

interface ServiceSchema<S = ServiceSettingSchema, T = void> {
interface ServiceSchema<S = ServiceSettingSchema, T = Service<S>> {
name: string;
version?: string | number;
settings?: S;
Expand All @@ -737,9 +735,9 @@ declare namespace Moleculer {
hooks?: ServiceHooks;

events?: ServiceEvents;
created?: ServiceSyncLifecycleHandler<S, T> | ServiceSyncLifecycleHandler<S, T>[];
started?: ServiceAsyncLifecycleHandler<S, T> | ServiceAsyncLifecycleHandler<S, T>[];
stopped?: ServiceAsyncLifecycleHandler<S, T> | ServiceAsyncLifecycleHandler<S, T>[];
created?: ServiceSyncLifecycleHandler<T> | ServiceSyncLifecycleHandler<T>[];
started?: ServiceAsyncLifecycleHandler<T> | ServiceAsyncLifecycleHandler<T>[];
stopped?: ServiceAsyncLifecycleHandler<T> | ServiceAsyncLifecycleHandler<T>[];

[name: string]: any;
}
Expand Down
77 changes: 75 additions & 2 deletions test/typescript/tsd/ServiceSchema_lifecycles.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { expectType } from "tsd";
import { Service, ServiceBroker, ServiceSchema } from "../../../index";
import {expectAssignable, expectType} from "tsd";
import {
Service, ServiceAsyncLifecycleHandler,
ServiceBroker,
ServiceSchema,
ServiceSettingSchema,
ServiceSyncLifecycleHandler
} from "../../../index";

const broker = new ServiceBroker({ logger: false, transporter: "fake" });

Expand Down Expand Up @@ -42,6 +48,39 @@ class TestService3 extends Service {
}
}

interface TestService4SettingSchema {
testService4Setting: string;
}

const testService4Schema: ServiceSchema<TestService4SettingSchema> = {
name: "test4",
settings: {
testService4Setting: "testService4"
},
}

interface TestService5Methods {
testService5Method: () => void;
}


interface TestService5SettingSchema {
testService5Setting: string;
}

interface TestService5This extends Service<TestService5SettingSchema>, TestService5Methods {}

const testService5Schema: ServiceSchema<TestService5SettingSchema, TestService5This> = {
name: "test5",
settings: {
testService5Setting: "testService5"
},
methods: {
testsService5Method: () => {},
}
}


const testService1 = new TestService1(broker);
expectType<ServiceSchema>(testService1.schema);

Expand All @@ -50,3 +89,37 @@ expectType<ServiceSchema>(testService2.schema);

const testService3 = new TestService3(broker);
expectType<ServiceSchema>(testService3.schema);

// Ensure that the lifecycle handlers are typed correctly when "This" type is not provided to service schema type
expectType<
ServiceSyncLifecycleHandler<Service<TestService4SettingSchema>> |
ServiceSyncLifecycleHandler<Service<TestService4SettingSchema>>[] |
undefined
>(testService4Schema.created)
expectType<
ServiceAsyncLifecycleHandler<Service<TestService4SettingSchema>> |
ServiceAsyncLifecycleHandler<Service<TestService4SettingSchema>>[] |
undefined
>(testService4Schema.started)
expectType<
ServiceAsyncLifecycleHandler<Service<TestService4SettingSchema>> |
ServiceAsyncLifecycleHandler<Service<TestService4SettingSchema>>[] |
undefined
>(testService4Schema.stopped)

// Ensure that the lifecycle handlers are typed correctly when "This" type is provided to service schema type
expectType<
ServiceSyncLifecycleHandler<TestService5This> |
ServiceSyncLifecycleHandler<TestService5This>[] |
undefined
>(testService5Schema.created)
expectType<
ServiceAsyncLifecycleHandler<TestService5This> |
ServiceAsyncLifecycleHandler<TestService5This>[] |
undefined
>(testService5Schema.started)
expectType<
ServiceAsyncLifecycleHandler<TestService5This> |
ServiceAsyncLifecycleHandler<TestService5This>[] |
undefined
>(testService5Schema.stopped)

0 comments on commit 3375833

Please sign in to comment.