Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Update default type for ServiceSchema generic to Service<S>. #1299

Merged
merged 2 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading