diff --git a/test/typescript/tsd/ServiceSchema_lifecycles.test-d.ts b/test/typescript/tsd/ServiceSchema_lifecycles.test-d.ts index b6486884b..93aecc01b 100644 --- a/test/typescript/tsd/ServiceSchema_lifecycles.test-d.ts +++ b/test/typescript/tsd/ServiceSchema_lifecycles.test-d.ts @@ -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" }); @@ -42,6 +48,39 @@ class TestService3 extends Service { } } +interface TestService4SettingSchema { + testService4Setting: string; +} + +const testService4Schema: ServiceSchema = { + name: "test4", + settings: { + testService4Setting: "testService4" + }, +} + +interface TestService5Methods { + testService5Method: () => void; +} + + +interface TestService5SettingSchema { + testService5Setting: string; +} + +interface TestService5This extends Service, TestService5Methods {} + +const testService5Schema: ServiceSchema = { + name: "test5", + settings: { + testService5Setting: "testService5" + }, + methods: { + testsService5Method: () => {}, + } +} + + const testService1 = new TestService1(broker); expectType(testService1.schema); @@ -50,3 +89,37 @@ expectType(testService2.schema); const testService3 = new TestService3(broker); expectType(testService3.schema); + +// Ensure that the lifecycle handlers are typed correctly when "This" type is not provided to service schema type +expectType< + ServiceSyncLifecycleHandler> | + ServiceSyncLifecycleHandler>[] | + undefined +>(testService4Schema.created) +expectType< + ServiceAsyncLifecycleHandler> | + ServiceAsyncLifecycleHandler>[] | + undefined +>(testService4Schema.started) +expectType< + ServiceAsyncLifecycleHandler> | + ServiceAsyncLifecycleHandler>[] | + undefined +>(testService4Schema.stopped) + +// Ensure that the lifecycle handlers are typed correctly when "This" type is provided to service schema type +expectType< + ServiceSyncLifecycleHandler | + ServiceSyncLifecycleHandler[] | + undefined +>(testService5Schema.created) +expectType< + ServiceAsyncLifecycleHandler | + ServiceAsyncLifecycleHandler[] | + undefined +>(testService5Schema.started) +expectType< + ServiceAsyncLifecycleHandler | + ServiceAsyncLifecycleHandler[] | + undefined +>(testService5Schema.stopped)