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

Fix mapUri type definition #137

Merged
merged 1 commit into from
Aug 10, 2023
Merged

Fix mapUri type definition #137

merged 1 commit into from
Aug 10, 2023

Conversation

joshkel
Copy link
Contributor

@joshkel joshkel commented Aug 10, 2023

It's used with await, so it may return a promise or non-promise value. The examples in API.md return a non-promise.

It's used with `await`, so it may return a promise or non-promise value.
The examples in API.md return a non-promise.
@Marsup
Copy link
Contributor

Marsup commented Aug 10, 2023

In general, I think it's best not to use polymorphic functions, so while it's possible, I'm not sure it should be encouraged.

@Marsup Marsup added the types TypeScript type definitions label Aug 10, 2023
@joshkel
Copy link
Contributor Author

joshkel commented Aug 10, 2023

@Marsup Thanks for the reply.

Maybe this is just a difference in terminology, but I've never heard this referred to as a polymorphic function. In TypeScript, I've understood polymorphic functions to be more or less a synonym for what the TypeScript documentation calls function overloading. From their docs:

function makeDate(timestamp: number): Date;
function makeDate(m: number, d: number, y: number): Date;
function makeDate(mOrTimestamp: number, d?: number, y?: number): Date {
  if (d !== undefined && y !== undefined) {
    return new Date(y, mOrTimestamp, d);
  } else {
    return new Date(mOrTimestamp);
  }
}

I agree that that style of API design can be confusing. However, this case - a function that can be synchronous or asynchronous, as expressed by a TypeScript return value that's a union of T | Promise<T> - is much simpler and more limited. It's directly supported by JavaScript's await, and it nicely simplifies code (which would otherwise have to add a spurious Promise.resolve calls on every synchronous return value). It's also widely used by Hapi itself - lifecycle methods such as route handlers and failAction methods are documented as returning either a value or a promise that resolves to a value, and plugins' register methods use the same technique to allow them to be synchronous or asynchronous without triggering a type error.

@Marsup Marsup added this to the 10.0.3 milestone Aug 10, 2023
@Marsup Marsup self-assigned this Aug 10, 2023
@Marsup
Copy link
Contributor

Marsup commented Aug 10, 2023

Makes sense. Thanks!

@Marsup Marsup merged commit f7d77a0 into hapijs:master Aug 10, 2023
18 checks passed
@joshkel joshkel deleted the mapuri-type branch August 10, 2023 17:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
types TypeScript type definitions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants