arktypea
arktype11mo ago
jhechtf

TypeScript complaining about passing a type from a custom scope to custom helper function

I've got a bit of a wrapper around Arktype that i work with, honestly a very simple function

export function validateObject<T extends Type<any>>(
  obj: unknown,
  validator: T,
): T extends Type<infer R> ? R : never {
  const data = validator(obj);

  if (data instanceof type.errors) throw data;

  return data;
}


Recently in a project I tried to use it with a custom scope and found that while it did run just fine, TypeScript is really mad about the types not matching for the validator object something like this

const myScope = scope({
      customId: 'string & /[A-HJKMNP-TV-Z]{26}/',
    });

    const typeWithCustomId = myScope.type({
      id: 'customId',
    });

    const data = validateObject(
      { id: 'A'.repeat(26) },
      typeWithCustomId,
    );

The picture is the error, which happens over typesWithCustomId
How can I type this helper function so that TypeScript isn't upset at it's usage?
image.png
Was this page helpful?