Use overloaded function with default initializer

Hi I’m trying to overload a function that change return type on parameter

isValidPropertyValue(propertyName: string, value: any,
    options?: { errors: true }): ValidationWithErrors;
  isValidPropertyValue(propertyName: string, value: any,
    options?: { errors: false }): boolean
  isValidPropertyValue(propertyName: string, value: any,
    options = { errors: false }): boolean | ValidationWithErrors {
    if (typeOf(propertyName) !== "string") throw new Error("propertyName must be a string");

    const conditions = this.properties[propertyName];
    if (!conditions) {
      return options.errors ? {
        valid: false,
        errors: [{
          property: propertyName,
          error: `Unauthorized property '${propertyName}'`
        }]
      } : false;
    }

    const errors: PropertyError[] = [];
    for (const [condition, validator] of Object.entries(propertyValidators)) {
      if (conditions[condition]) {
        try {
          validator(conditions[condition], value);
        } catch (e: any) {
          errors.push({ property: propertyName, error: e.message });
        }
      }
    }

    return options.errors ? { valid: !errors.length, errors } : !errors.length;
  }

I have set a default initializer on the option parameter like you can see but when I’m trying to call the function I got an error

TS2554: Expected 3 arguments, but got 2.

Futhermore it doesn’t change my return type and keep it on ValidationWithError even if I set the option to false

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I’m not sure how you go the argument error. TS2554: Expected 3 arguments, but got 2. I wasn’t able to reproduce that with your code. But if you remove the ? from the first signature, TS will correctly infer the return type. It makes sense because when options is undefined or { errors: false }, you want to return a boolean but you only return ValidationErrors when it’s { errors: true } and not when it’s undefined.

interface ValidationWithErrors {
  valid: boolean;
  errors: { property: string; error: string }[];
}

function isValidPropertyValue(
  propertyName: string,
  value: any,
  options: { errors: true }
): ValidationWithErrors;
function isValidPropertyValue(
  propertyName: string,
  value: any,
  options?: { errors: false }
): boolean;
function isValidPropertyValue(
  propertyName: string,
  value: any,
  options = { errors: false }
): boolean | ValidationWithErrors {
  if (options.errors) {
    return {
      valid: false,
      errors: [{ property: propertyName, error: 'invalid value' }],
    };
  }
  return false;
}

const isValid = isValidPropertyValue('property', 'value'); // boolean
const validationWithErrors = isValidPropertyValue('property', 'value', {
  errors: true,
}); // ValidationWithErrors


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x