infer return type of functions

This commit is contained in:
Thomas Ruoff
2021-03-22 23:14:48 +01:00
parent 3cf2aff832
commit 9fe3fffb86
16 changed files with 54 additions and 54 deletions

View File

@@ -18,10 +18,10 @@ export interface Bill {
export interface BillDocument
extends Bill,
mongoose.SchemaTimestampsConfig,
mongoose.Document {}
mongoose.SchemaTimestampsConfig,
mongoose.Document { }
export interface BillModel extends mongoose.Model<BillDocument> {}
export interface BillModel extends mongoose.Model<BillDocument> { }
const BillSchema = new mongoose.Schema<BillDocument>(
{
@@ -29,7 +29,7 @@ const BillSchema = new mongoose.Schema<BillDocument>(
type: Number,
required: true,
validate: {
validator: function (v: number) {
validator: function(v: number): boolean {
const bill = this as BillDocument
return v <= bill.milageEnd
@@ -43,7 +43,7 @@ const BillSchema = new mongoose.Schema<BillDocument>(
required: true,
validate: {
validator: function (v: number) {
validator: function(v: number): boolean {
const bill = this as BillDocument
return v >= bill.milageStart
@@ -77,12 +77,12 @@ const BillSchema = new mongoose.Schema<BillDocument>(
}
)
BillSchema.virtual('milage').get(function () {
BillSchema.virtual('milage').get(function(): number {
const bill = this as BillDocument
return bill.milageEnd - bill.milageStart
})
BillSchema.virtual('total').get(function () {
BillSchema.virtual('total').get(function(): number {
const bill = this as BillDocument
return getBillTotal(bill)

View File

@@ -22,8 +22,8 @@ export interface Booking {
export interface BookingDocument
extends Booking,
mongoose.Document,
mongoose.SchemaTimestampsConfig {}
mongoose.Document,
mongoose.SchemaTimestampsConfig { }
export interface BookingModel extends mongoose.Model<BookingDocument> {
findBookedDays(uuidsToIngore?: string[]): Promise<string[]>
@@ -52,10 +52,10 @@ const BookingSchema = new mongoose.Schema<BookingDocument>(
required: true,
get: dateFormatBackend,
validate: {
validator: function (v: Date) {
validator: function(v: Date): boolean {
return v >= nowInTz()
},
message: (props: { value: Date }) => `${props.value} is in the past`,
message: (props: { value: Date }): string => `${props.value} is in the past`,
},
},
endDate: {
@@ -63,27 +63,27 @@ const BookingSchema = new mongoose.Schema<BookingDocument>(
required: false,
get: dateFormatBackend,
validate: {
validator: function (v: Date) {
validator: function(v: Date): boolean {
return v >= nowInTz()
},
message: (props: { value: Date }) => `${props.value} is in the past`,
message: (props: { value: Date }): string => `${props.value} is in the past`,
},
},
days: {
type: [String],
required: true,
validate: {
validator: async function (days: string[]) {
validator: async function(days: string[]): Promise<boolean> {
const booking = this as Booking
const uuid = booking.uuid && [booking.uuid]
const bookedDays = await getBookedDays(uuid)
const doubleBookedDays = days.filter((day: string) =>
const doubleBookedDays = days.filter((day: string): boolean =>
bookedDays.includes(day)
)
return doubleBookedDays.length === 0
},
message: (props: { value: string[] }) =>
message: (props: { value: string[] }): string =>
`At least one day is of ${props.value.join(',')} is already booked`,
type: VALIDATION_ERRORS.AT_LEAST_ONE_DAY_BOOKED,
},
@@ -105,7 +105,7 @@ const BookingSchema = new mongoose.Schema<BookingDocument>(
}
)
BookingSchema.pre('validate', function (next: () => void) {
BookingSchema.pre('validate', function(next: () => void): void {
const booking = this as BookingDocument
booking.days = getDays({
startDate: new Date(booking.startDate),
@@ -114,7 +114,7 @@ BookingSchema.pre('validate', function (next: () => void) {
next()
})
BookingSchema.static('findBookedDays', async function (
BookingSchema.static('findBookedDays', async function(
uuidsToIngore: string[] = []
): Promise<string[]> {
const model = this as BookingModel

View File

@@ -1,12 +1,12 @@
import * as mongoose from 'mongoose'
import BookerModel, { Booker } from './booker'
import BookingModel, { Booking } from './booking'
import BookingModel, { Booking, BookingDocument } from './booking'
import BillModel, { Bill } from './bill'
import { BOOKING_STATUS } from './enums'
let connectedPromise: Promise<typeof mongoose>
function connect() {
function connect(): Promise<typeof mongoose> {
if (connectedPromise) {
return
}
@@ -20,12 +20,12 @@ function connect() {
return connectedPromise
}
export async function getBookedDays(uuidsToIngore?: string[]) {
export async function getBookedDays(uuidsToIngore?: string[]): Promise<string[]> {
await connect()
return BookingModel.findBookedDays(uuidsToIngore)
}
export async function getBookingByUUID(uuid: string) {
export async function getBookingByUUID(uuid: string): Promise<BookingDocument> {
await connect()
return BookingModel.findOne({ uuid })
}
@@ -33,7 +33,7 @@ export async function getBookingByUUID(uuid: string) {
export async function getBookings({
status = [BOOKING_STATUS.CONFIRMED, BOOKING_STATUS.REQUESTED],
startDateGreaterThan = '2000-01-01T00:00:00Z',
}: { status?: BOOKING_STATUS[]; startDateGreaterThan?: string } = {}) {
}: { status?: BOOKING_STATUS[]; startDateGreaterThan?: string } = {}): Promise<BookingDocument[]> {
await connect()
return await BookingModel.find({
status: { $in: status },