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

Middleware type definitions don’t match actual API #2360

Open
Jaid opened this issue Sep 16, 2023 · 1 comment
Open

Middleware type definitions don’t match actual API #2360

Jaid opened this issue Sep 16, 2023 · 1 comment

Comments

@Jaid
Copy link

Jaid commented Sep 16, 2023

It’s currently not that easy to write middlewares in TypeScript, because the actual API and exported types seem to be out of sync.


This is a legitimate Yargs middleware:

type MiddlewareFunction = (args: ArgumentsCamelCase, yargs: YargsInstance) => Record<string, unknown>

Which can be verified by looking directly at the API code.

yargs/lib/middleware.ts

Lines 111 to 114 in e517318

const result = middleware(acc, yargs);
return isPromise(result)
? result.then(middlewareObj => Object.assign(acc, middlewareObj))
: Object.assign(acc, result);

However, this is the corresponding type definition I am consuming:

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/3a6d407361cc6025d136ae5eb3c3e3e24ada5009/types/yargs/v16/index.d.ts#L838


The problems with that:

  • The second parameter of MiddlewareFunction is not documented.
  • Type YargsInstance doesn’t seem to be accessible.
  • The callback return type is void, but the API user can actually decide between modifying the original args (args.foo = 'bar'; return) and letting Yargs do the merging (const changes = {}; changes.foo = 'bar'; return changes).
@shadowspawn
Copy link
Member

The types available for client use are from DefinitelyTyped, and maintained separately. You might want to open an issue/discussion over there too.

For comparison, the definition used internally in yargs is:

yargs/lib/middleware.ts

Lines 121 to 125 in e517318

export interface MiddlewareCallback {
(argv: Arguments, yargs: YargsInstance):
| Partial<Arguments>
| Promise<Partial<Arguments>>;
}

And for interest related to the internal typings: #1751

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants