Skip to content

TypeScript reports errors with skipWhen #1156

@HymanZHAN

Description

@HymanZHAN

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions