Skip to content

TypeScript Support

Ctrovalidate is written in standard JavaScript with deep JSDoc integration, providing first-class IntelliSense and type safety for both JavaScript and TypeScript developers.

📦 Typings

Type definition files are included in the package under dist/types. They are automatically detected by modern IDEs and compilers.

🛠️ Usage in TypeScript

Basic Initialization

typescript
import { Ctrovalidate, type CtrovalidateOptions } from 'ctrovalidate';

const form = document.querySelector<HTMLFormElement>('#my-form')!;

const options: CtrovalidateOptions = {
  realTime: true,
  logLevel: Ctrovalidate.LogLevel.INFO,
  errorClass: 'has-error',
};

const validator = new Ctrovalidate(form, options);

Custom Rule Types

When adding custom rules, you can benefit from the imported RuleLogic or AsyncRuleLogic types.

typescript
import { Ctrovalidate, type RuleLogic } from 'ctrovalidate';

const myLogic: RuleLogic = (value, params, field) => {
  return value.includes('special');
};

Ctrovalidate.addRule('myRule', myLogic, 'Error message');

🧩 Key Types Reference

TypeDescription
CtrovalidateOptionsThe configuration object for the constructor.
LogLevelEnum values for console output.
ValidationResultThe internal object representing a field's status.
RuleLogicFunction signature for synchronous custom rules.
AsyncRuleLogicFunction signature for asynchronous custom rules.

TIP

You don't need to manually install @types/ctrovalidate. The library includes them out of the box.

Next Steps

Released under the MIT License.