From 0b0ef526061664935053fc030973e4f14a3d6c68 Mon Sep 17 00:00:00 2001 From: Deadly0 Date: Mon, 5 Nov 2018 20:13:11 +0300 Subject: [PATCH 1/4] Improve Enum typing, add helpers. --- types/meteor-astronomy/index.d.ts | 21 +++++--- .../meteor-astronomy-tests.ts | 54 ++++++++++++++----- 2 files changed, 56 insertions(+), 19 deletions(-) diff --git a/types/meteor-astronomy/index.d.ts b/types/meteor-astronomy/index.d.ts index 2bef7e728c..7cfd2c7211 100644 --- a/types/meteor-astronomy/index.d.ts +++ b/types/meteor-astronomy/index.d.ts @@ -7,6 +7,11 @@ /// declare namespace MeteorAstronomy { + type NonFunctionPropertyNames = { [K in keyof T]: T[K] extends Function ? never : K }[keyof T]; // tslint:disable-line:ban-types + type NonFunctionProperties = Pick>; + type FunctionPropertyNames = { [K in keyof T]: T[K] extends Function ? K : never }[keyof T]; // tslint:disable-line:ban-types + type FunctionProperties = Pick>; + type TypeOptionsPrimitives = typeof String | typeof Date | typeof Boolean | typeof Object | typeof Number; type TypeOptions = TypeOptionsPrimitives | TypeOptionsPrimitives[] | Class | Enum; type MongoQuery = object | string; @@ -40,7 +45,11 @@ declare namespace MeteorAstronomy { type ModelField = ModelFullField | TypeOptions; type Fields = { - [P in keyof T]: ModelField; + [P in keyof NonFunctionProperties]: ModelField; + }; + + type Helpers = { + [P in keyof FunctionProperties]: (this: T, ...args: any) => any; }; interface ClassModel { @@ -53,7 +62,7 @@ declare namespace MeteorAstronomy { update: boolean, remove: boolean, } | boolean; - helpers?: object; + helpers?: Helpers; events?: object; meteorMethods?: object; indexes?: object; @@ -61,7 +70,7 @@ declare namespace MeteorAstronomy { interface EnumModel { name: string; - identifiers: T[] | object; + identifiers: string[] | T; } type Model = T & { @@ -92,10 +101,10 @@ declare namespace MeteorAstronomy { update(search: object | string, query: object, callback?: () => void): void; } - interface Enum { + type Enum = T & { getValues(): any[]; - getIdentifier(identifier: T): any; - } + getIdentifier(identifier: any): string; + }; } declare module 'meteor/jagi:astronomy' { // tslint:disable-line:no-single-declare-module diff --git a/types/meteor-astronomy/meteor-astronomy-tests.ts b/types/meteor-astronomy/meteor-astronomy-tests.ts index 871c02964b..59f0f99644 100644 --- a/types/meteor-astronomy/meteor-astronomy-tests.ts +++ b/types/meteor-astronomy/meteor-astronomy-tests.ts @@ -65,8 +65,11 @@ const UserProfile = Class.create({ interface UserInterface extends Meteor.User { address: object; + firstName: string; + lastName: string; phone: string; phoneNumber: string; + fullName: (param: string) => string; } const User = Class.create({ @@ -74,9 +77,11 @@ const User = Class.create({ collection: Meteor.users as Mongo.Collection, fields: { createdAt: Number, + firstName: String, + lastName: String, emails: { type: [Object], - default: () => [], + default: (): Meteor.UserEmail[] => [], }, profile: { type: UserProfile, @@ -91,10 +96,23 @@ const User = Class.create({ }, phone: { type: String, - resolve(doc) { + resolve(doc: UserInterface) { return doc.phoneNumber; + }, + }, + }, + helpers: { + fullName(param: string): string { + const fullName = `${this.firstName} ${this.lastName}`; + + if (param === 'lower') { + return fullName.toLowerCase(); + } else if (param === 'upper') { + return fullName.toUpperCase(); } - } + + return fullName; + }, }, indexes: { fullName: { // Index name. @@ -111,13 +129,16 @@ const user = User.findOne(); user.set({username: 'user1'}); user.save(); -enum IStatus { - OPENED, CLOSED, DONE, CANCELED +interface StatusInterface { + OPENED: number; + CLOSED: number; + DONE: number; + CANCELED: number; } -const Status = Enum.create({ +const Status = Enum.create({ name: 'Status', - identifiers: IStatus, + identifiers: ['OPENED', 'CLOSED', 'DONE', 'CANCELED'], }); const Issue = Class.create({ @@ -131,18 +152,25 @@ const Issue = Class.create({ Status.getValues(); // [0, 1, 2, 3] -const StatusBis = Enum.create({ +interface StatusBisInterface { + OPENED: string; + CLOSED: string; + DONE: string; + CANCELED: string; +} + +const StatusBis = Enum.create({ name: 'Status', identifiers: { - OPENED: 5, - CLOSED: null, - DONE: 15, - CANCELED: undefined + OPENED: 'OPENED', + CLOSED: 'CLOSED', + DONE: 'DONE', + CANCELED: 'CANCELED', } }); StatusBis.getValues(); // [5, 6, 15, 16] -const statusNumber = IStatus.OPENED; +const statusNumber = Status.OPENED; Status.getIdentifier(statusNumber); // "OPENED" From ca9a6163dd8e1813535d47749fc7bcbdb881730e Mon Sep 17 00:00:00 2001 From: Deadly0 Date: Mon, 5 Nov 2018 20:20:05 +0300 Subject: [PATCH 2/4] Fix ts version. --- types/meteor-astronomy/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/meteor-astronomy/index.d.ts b/types/meteor-astronomy/index.d.ts index 7cfd2c7211..485b1d9a39 100644 --- a/types/meteor-astronomy/index.d.ts +++ b/types/meteor-astronomy/index.d.ts @@ -2,7 +2,7 @@ // Project: https://github.com/jagi/meteor-astronomy/ // Definitions by: Igor Golovin // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.8 +// TypeScript Version: 3.1 /// From d850bb85bac2f87da3ff4c2d193d4a881d5c48bd Mon Sep 17 00:00:00 2001 From: Deadly0 Date: Mon, 5 Nov 2018 20:25:51 +0300 Subject: [PATCH 3/4] Add package.json --- types/meteor-astronomy/index.d.ts | 1 - types/meteor-astronomy/package.json | 8 ++++++++ 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 types/meteor-astronomy/package.json diff --git a/types/meteor-astronomy/index.d.ts b/types/meteor-astronomy/index.d.ts index 485b1d9a39..5c5771cb9b 100644 --- a/types/meteor-astronomy/index.d.ts +++ b/types/meteor-astronomy/index.d.ts @@ -2,7 +2,6 @@ // Project: https://github.com/jagi/meteor-astronomy/ // Definitions by: Igor Golovin // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 3.1 /// diff --git a/types/meteor-astronomy/package.json b/types/meteor-astronomy/package.json new file mode 100644 index 0000000000..9d81651687 --- /dev/null +++ b/types/meteor-astronomy/package.json @@ -0,0 +1,8 @@ +{ + "name": "meteor-astronomy", + "version": "2.6", + "types": "./index.d.ts", + "typesVersions": { + ">=3.1": { "*": ["ts3.1/*"] } + } +} From e295c455a5b03a8f35df1046046f920324790b26 Mon Sep 17 00:00:00 2001 From: Deadly0 Date: Mon, 5 Nov 2018 20:38:29 +0300 Subject: [PATCH 4/4] Back to ts 2.8 --- types/meteor-astronomy/index.d.ts | 3 ++- types/meteor-astronomy/package.json | 8 -------- 2 files changed, 2 insertions(+), 9 deletions(-) delete mode 100644 types/meteor-astronomy/package.json diff --git a/types/meteor-astronomy/index.d.ts b/types/meteor-astronomy/index.d.ts index 5c5771cb9b..c17c1d1498 100644 --- a/types/meteor-astronomy/index.d.ts +++ b/types/meteor-astronomy/index.d.ts @@ -2,6 +2,7 @@ // Project: https://github.com/jagi/meteor-astronomy/ // Definitions by: Igor Golovin // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.8 /// @@ -48,7 +49,7 @@ declare namespace MeteorAstronomy { }; type Helpers = { - [P in keyof FunctionProperties]: (this: T, ...args: any) => any; + [P in keyof FunctionProperties]: (this: T, ...args: any[]) => any; }; interface ClassModel { diff --git a/types/meteor-astronomy/package.json b/types/meteor-astronomy/package.json deleted file mode 100644 index 9d81651687..0000000000 --- a/types/meteor-astronomy/package.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "name": "meteor-astronomy", - "version": "2.6", - "types": "./index.d.ts", - "typesVersions": { - ">=3.1": { "*": ["ts3.1/*"] } - } -}