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

Wraps function types in additional brackets to prevent broken union types #400

Merged
merged 2 commits into from
Dec 10, 2023

Conversation

cmaster11
Copy link
Collaborator

@cmaster11 cmaster11 commented Dec 10, 2023

E.g. take this schema:

export const AlternativesWithFunctionSchema = Joi.alternatives([
  Joi.function().minArity(2),
  Joi.object({
    json: Joi.any().required()
  }),
  Joi.object({
    raw: Joi.string().required()
  })
]).meta({ className: 'AlternativesWithFunctionInterface' });

Without brackets, a chain would have become

(...args: any[]) => any | {
  json: any;
} | {
  raw: string;
};

In this type, the whole any | { json: any; } | { raw: string; } is the return type of the function, which is wrong.

With brackets, it will be:

export type AlternativesWithFunctionInterface = ((...args: any[]) => any) | {
  json: any;
} | {
  raw: string;
};

There could be some more advanced logic to figure out if we want the brackets or not depending if we're using a union or not, but I feel it is even safer in general to just wrap function types in brackets given the "unsupported" nature of the Function type.

Note: this will conflict on merge with #401. I'll take care of it if merged. Probably one test will fail.

Copy link

codecov bot commented Dec 10, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (e80f93f) 98.69% compared to head (f7c6bf9) 98.69%.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #400   +/-   ##
=======================================
  Coverage   98.69%   98.69%           
=======================================
  Files           9        9           
  Lines         535      535           
  Branches      206      206           
=======================================
  Hits          528      528           
  Misses          7        7           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@cmaster11 cmaster11 marked this pull request as draft December 10, 2023 06:25
@cmaster11 cmaster11 marked this pull request as ready for review December 10, 2023 06:27
@mrjono1 mrjono1 merged commit 4366519 into mrjono1:master Dec 10, 2023
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants