Skip to content
  1. Extras
  2. FetchIt
  3. JS API
  4. TypeScript

TypeScript ​

The types live in assets/components/fetchit/js/fetchit.d.ts. Copy the file into your project or reference it:

ts
/// <reference path="../assets/components/fetchit/js/fetchit.d.ts" />

document.addEventListener('fetchit:error', event => {
  // response is null when no FetchIt answer came: network, someone else's answer, a captcha with no answer
  if (event.detail.response === null) {
    console.error(event.detail.error)
  }
})

const form = document.querySelector('form')
if (form) {
  FetchIt.instances.get(form)?.setError('email', 'Check the address')
}

The file describes FetchIt and its static members, the config of FetchIt.create(), a form instance with all its methods, FetchIt.Message and the detail of every event. The script of the component is type-checked against this same file, so the types and the code do not drift apart.

Внимание

Remove any FetchIt declaration of your own (declare var FetchIt: any): the two would clash.

What is described ​

TypeWhat it is
FetchItStaticthe global FetchIt: forms, instances, Message, events, create(), createNotifier() and the rest
FetchItConfigthe config of a form, which the snippet passes to FetchIt.create()
FetchItInstancea form instance: setError(), clearErrors(), setFormMessage(), getFields() and others
FetchItResponsethe answer of the server: success, message, data
FetchItMessagethe notification hooks before, after, success, error, reset
FetchItNotifierOptionsthe options of FetchIt.createNotifier()
FetchItBeforeDetail and the other *Detail typesthe detail of each event

The event map is added to DocumentEventMap, so document.addEventListener('fetchit:success', …) knows the type of its event.detail without a cast.