precognition
precognition copied to clipboard
Provided signature and partial type compatibility with InertiaJS
I did some rework on the vue-inertia package in order to make it compatible with the original InertiaJS "useForm":
Changes
- Precognition "useForm" will accept object type like InertiaJS:
import {useForm} from 'laravel-precognition-vue-inertia'
const form = useForm<MyInterface>('post', 'https://example.net', mydata);
// or useForm('post', 'https://example.net', mydata as MyInterface);
-
Precognition "useForm" will return the original InertiaJS properties and methods patched with the Precognition properties and methods (I added the types).
-
Original interface properties are also exposed.
Advantages of this change
- Original interface properties and Precognition properties and methods are exposed to the linter.
- Better Typescript support.
- Increased signature compatibility with InertiaJS.
- It doesn't require any change in the Precognition core libraries or precognition-vue package.
Disadvantages of this change
- It may require to update the return type in case that original InertiaJS "useForm" method change any of their return properties or method.
@driesvints: This is a better solution of https://github.com/laravel/precognition/pull/64 but also resolves https://github.com/laravel/precognition/pull/55.
Another alternative it's also to the extend the original InertiaForm:
Example:
type FormDataType = object;
interface PrecognitionFormProps<TForm> {
validating: boolean,
progress: Progress | null;
reset(...fields: (keyof TForm)[]): this;
clearErrors(...fields: (keyof TForm)[]): this;
touched(name: string): boolean,
touch(name: string | string[] | NamedInputEvent): this;
submit(submitMethod: RequestMethod|Config, submitUrl?: string, submitOptions?: Partial<VisitOptions>): void;
setErrors(errors: SimpleValidationErrors|ValidationErrors) : this;
forgetError(name: string|NamedInputEvent): this;
setError(field: keyof TForm, value: string): this;
validate(name?: string|NamedInputEvent): this;
setValidationTimeout(duration: number): this;
validateFiles(): this;
validator(): Validator;
valid(name: string | NamedInputEvent | string[]): boolean,
invalid(name: string): boolean,
}
type PrecognitionForm<TForm extends FormDataType> = TForm & PrecognitionFormProps<TForm> & InertiaForm<TForm>;
The advantage is that in case that InertiaJS implement new props it will be inhered, however the disadvantage is that it's not possible to hide non compatible methods and props.
Thanks @juanparati. Will let @timacdonald review this one.