From 6c2ae3b20aeb588105b32dee1754dc20f42e2c22 Mon Sep 17 00:00:00 2001 From: Desmond Koh Date: Fri, 31 May 2019 02:45:36 +0800 Subject: [PATCH] Improved service typings. (#35688) * Updated @feathersjs/socket-commons * Fixed linting errors. * Added type definitions for hook-less service methods. * Revert "Added type definitions for hook-less service methods." This reverts commit 14e8062a57f68b7c338ee092fb3e5cfea9e6f226. See https://github.com/DefinitelyTyped/DefinitelyTyped/pull/34684 * Improved service typings. * Fixed tests. * Fixed lint. * Fixed typo. --- .../feathersjs__feathers-tests.ts | 12 ++++++++++++ types/feathersjs__feathers/index.d.ts | 11 ++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/types/feathersjs__feathers/feathersjs__feathers-tests.ts b/types/feathersjs__feathers/feathersjs__feathers-tests.ts index 1fa02a79fb..03e6f027f0 100644 --- a/types/feathersjs__feathers/feathersjs__feathers-tests.ts +++ b/types/feathersjs__feathers/feathersjs__feathers-tests.ts @@ -33,3 +33,15 @@ app.service('users').hooks({ context.dispatch = { test: 'true' }; } }); + +app.service('users').get(0); // app: Service +let x = app.service('user'); // never +x = (() => { throw new Error(); })(); // only never can be assigned to never + +const app2 = feathers(); +app2.service('users').get(0); // app2: Service +app2.service('user').get(0); // app2 :Service + +const app3 = feathers<{}>(); +app3.service('users').get(0); // Service +app3.service('user').get(0); // Service diff --git a/types/feathersjs__feathers/index.d.ts b/types/feathersjs__feathers/index.d.ts index 0cfacbab43..51e333587e 100644 --- a/types/feathersjs__feathers/index.d.ts +++ b/types/feathersjs__feathers/index.d.ts @@ -4,9 +4,10 @@ // Abraao Alves // Tim Mensch // Jordan Tucker +// Desmond Koh // Definitions: https://github.com/feathersjs-ecosystem/feathers-typescript -// TypeScript Version: 2.3 +// TypeScript Version: 2.8 /// @@ -159,7 +160,7 @@ declare namespace feathers { } interface SetupMethod { - setup(app: Application, path: string): void; + setup(app: Application, path: string): void; } interface ServiceOverloads { @@ -176,7 +177,7 @@ declare namespace feathers { type Service = ServiceOverloads & ServiceAddons & ServiceMethods; - interface Application extends EventEmitter { + interface Application extends EventEmitter { get(name: string): any; set(name: string, value: any): this; @@ -197,9 +198,9 @@ declare namespace feathers { service(location: L): Service; - service(location: string): Service; + service(location: string): keyof ServiceTypes extends never ? Service : never; - use(path: string, service: Partial & SetupMethod> | Application, options?: any): this; + use(path: string, service: Partial & SetupMethod> | Application, options?: any): this; version: string; }