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

TypeScript reports errors with skipWhen #1156

Open
HymanZHAN opened this issue Dec 7, 2024 · 0 comments
Open

TypeScript reports errors with skipWhen #1156

HymanZHAN opened this issue Dec 7, 2024 · 0 comments

Comments

@HymanZHAN
Copy link

HymanZHAN commented Dec 7, 2024

First, thank you for creating this cool library. It offers flexibility that other schema-based solutions don't, and I've been enjoying it so far.

My issue is that when I try to follow the skipWhen example in the doc, my editor (TypeScript, to be more precise) starts complaining.

My example suite:

export const createSuite = (field: string, context: any) =>
  create((form: Partial<RegisterForm> = {}, field: string) => {
    only(field);

    // name
    test('name', 'Username is required', () => {
      enforce(form.name).isNotBlank();
    });

    test('name', 'Username must be at least 2 characters long', () => {
      enforce(form.name).longerThan(1);
    });

    test('name', 'Name can have at most 18 characters', () => {
      enforce(form.name).shorterThan(19);
    });

    skipWhen(
      (res) => res.hasErrors('name'),
      () => {
        test('name', `Name ${form.name} already taken`, () => {
          if (!context.userService) return Promise.resolve(true);
          const service = context.userService as UserService;
          return service.isUsernameUnique(form.name ?? '');
        });
      },
    );
  });

But TS is reporting errors on line "test('name', Name ${form.name} already taken, () => {":

Diagnostics:
1. No overload matches this call.
     Overload 1 of 4, '(fieldName: "name", message: string, cb: TestFn): TIsolateTest<string, string>', gave the following error.
       Argument of type '() => Promise<boolean>' is not assignable to parameter of type 'TestFn'.
         Type 'Promise<boolean>' is not assignable to type 'TestResult'.
           Type 'Promise<boolean>' is not assignable to type 'AsyncTest'.
             Type 'boolean' is not assignable to type 'void'.
     Overload 2 of 4, '(fieldName: "name", cb: TestFn, key: IsolateKey): TIsolateTest<string, string>', gave the following error.
       Argument of type 'string' is not assignable to parameter of type 'TestFn'. [2769

It also happens with the minimal example in the doc:

export default create((data = {}) => {
  test('username', 'Username is required', () => {
    enforce(data.username).isNotBlank();
  });

  skipWhen(
    (res) => res.hasErrors('username'),
    () => {
      test('username', 'Username already exists', async () => {
        // this is an example for a server call
        return true;
      });
    },
  );
});

image

Any help is appreciated. Thanks.

Versions

  • vest: 5.4.5
  • typescript: 5.5.4
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

No branches or pull requests

1 participant