Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve typescript types #40

Merged
merged 3 commits into from
Mar 9, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
import {RequestHandler, ErrorRequestHandler, NextFunction as OriginalNextFunction} from 'express-serve-static-core'

export = OpenApiEnforcerMiddleware

declare class OpenApiEnforcerMiddleware {
constructor (definition: string|object, options?:OpenApiEnforcerMiddleware.Options );

controllers (controllersDirectoryPath: string|object, ...dependencyInjection: any): Promise<object>;
controllers<T extends unknown[]> (controllersDirectoryPath: string | OpenApiEnforcerMiddleware.Controllers, ...dependencyInjection: T): Promise<object>;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please educate me on this one. Why <T extends unknown[]> instead of using any[]?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The short answer is that any isn't type safe so I always default to using unknown instead of any. It's kind of like using const by default even though you could use let to declare all variables in javascript.

You can read more about unknown vs any on this stackoverflow answer or on the Typescript 3.0 RC.

middleware (): OpenApiEnforcerMiddleware.MiddlewareFunction;
mocks (controllersDirectoryPath: string|object|undefined, automatic?: boolean, ...dependencyInjection: any): Promise<object>;
mocks<T extends unknown[]> (controllersDirectoryPath?: string | OpenApiEnforcerMiddleware.Controllers, automatic?: boolean, ...dependencyInjection: T): Promise<object>;
use (middleware: OpenApiEnforcerMiddleware.MiddlewareFunction): void;

promise: Promise<object>
}

declare namespace OpenApiEnforcerMiddleware {

export interface MiddlewareFunction {
(req: object, res: object, next: NextFunction): void;
(err: Error, req: object, res: object, next: NextFunction): void;
}
export type MiddlewareFunction = RequestHandler | ErrorRequestHandler

export interface NextFunction {
(err?: Error): void;
}
export type NextFunction = OriginalNextFunction

export type Controllers = Record<string, MiddlewareFunction>

export interface Options {
allowOtherQueryParameters?: boolean;
Expand Down
Loading