dwightjack / vue-types

Vue Prop Types definitions
https://dwightjack.github.io/vue-types/
MIT License
572 stars 35 forks source link

func cannot recognize async functions for validation. #397

Closed ilxqx closed 1 year ago

ilxqx commented 1 year ago

Describe the bug I have a piece of code as follows:

type BiConsumerFunction<T, U> = (t: T, u: U) => void;
type DataOperationButton<T = Recordable<GeneralValue>> = {
    name: string;
    key: string;
    disabled?: boolean | BiControlFunction<T, number>;
    show?: boolean | BiControlFunction<T, number>;
    loading?: BiControlFunction<T, number>;
    permission?: string;
    confirmable?: boolean | BiControlFunction<T, number>;
    confirmationText?: string | BiMappingFunction<T, number, string>;
    onClick?: BiConsumerFunction<T, number>;
  };
}

export function buildDataOperationButtonShape<T = Recordable<GeneralValue>>() {
  return shape<DataOperationButton<T>>({
    name: string().isRequired,
    key: string().isRequired,
    disabled: oneOfType<NonNil<DataOperationButton<T>["disabled"]>>([bool(), func()]),
    show: oneOfType<NonNil<DataOperationButton<T>["show"]>>([bool(), func()]),
    loading: func<NonNil<DataOperationButton<T>["loading"]>>(),
    permission: string(),
    confirmable: oneOfType<NonNil<DataOperationButton<T>>["confirmable"]>([bool(), func()]),
    confirmationText: oneOfType<NonNil<DataOperationButton<T>>["confirmationText"]>([string(), func()]),
    onClick: func<NonNil<DataOperationButton<T>["onClick"]>>(),
  }).loose;
}

I put the important code together to form the above code. Let's focus on it. Here, when validating the onClick prop, if an async function is passed to it, a warning will be thrown at runtime:

arrayOf - value validation error:
  shape - "onClick" property validation error:
     function - value "async (rowData) => {
              await new Promise((res) => {
                setTimeout(res, 3e3);
              });
              console.log("modify", rowData);
            }" should be of type "Function"

Expected behavior func validation can support both regular functions and async functions.

Library version 5.0.3

Vue.js version 3.3.4

dwightjack commented 1 year ago

@ilxqx I published a fix in vue-types@5.0.4. Let me know if it works.

ilxqx commented 1 year ago

@dwightjack Yes, it's working very well, very thankful 👍🏻👍🏻👍🏻