Autocompletion & Typings
Since the Forest Admin agent is fully written in Typescript, every function has autocompletion capabilities. The agent can generate typing files based on your models.
This allows:
Autocompletion of collection names and fields
Typing validation on all your handlers (when using TypeScript)
Generate a typing file
The typing file generated by Forest Admin will allow you to autocomplete any given fields of a collection. This is an auto-generated file, so we highly suggest not editing it manually.
In the following sections, you can see that two options are given to the createAgent
function:
typingsPath
: The location you want your typing file to be created.typingsMaxDepth
: The maximum depth of introspection for relationships.
Using the typing file in a typescript context
In a typescript context, the usage of the typing file is pretty straightforward. You can simply import the typing file and template your createAgent
with the schema to use it.
import { createAgent } from '@forestadmin/agent';
import { Schema } from './typings';
import transactions from './customization/transactions';
createAgent<Schema>({
// ...
typingsPath: './typings.ts',
typingsMaxDepth: 5,
})
.customizeCollection('transactions', transactions)
.mountOnStandaloneServer(3000)
.start();
You should be able to see that the customizeCollection
method is strongly typed, as well as the transactions
handler.
Using the typing file in a JavaScript context
You can still benefit from it in a Javascript environment by using the JSDoc syntax.
If you choose to split your customizeCollection
handler in a dedicated file (That's the project scaffolding we usually use at Forest Admin), you can use the following JSDoc example to keep the autocomplete experience intact.
const { createAgent } = require('@forestadmin/agent');
const transactions = require('./customization/transactions');
/**
* @type {import('@forestadmin/agent').Agent<import('./typings').Schema>}
*/
const agent = createAgent({
//...
typingsMaxDepth: 5,
typingsPath: './typings.ts',
});
agent.customizeCollection('transactions', transactions).mountOnStandaloneServer(3000).start();
Last updated
Was this helpful?