diff --git a/types/meteor-astronomy/index.d.ts b/types/meteor-astronomy/index.d.ts index 2bef7e728c..c17c1d1498 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"