Expose types trough XP modules
Big changes
- Types must now be imported from the corresponding the XP-modules (like
"/lib/xp/content"
), instead of being imported from a separate library (like"enonic-types/content"
).
This is a cleaner approach, since the types now lives with the functions that use them. "enonic-types/controller"
doesn't have a corresponding library since models the shape of the internal structures in XP. So all the types there has been made available on the global scope under theXP
namespace.
Now you can just useXP.Request
without doing any imports.
To use this release you will need to change all the imports in your code like this:
- import { Request, Response } from "enonic-types/controller";
- import { get } from "/lib/xp/content";
- import { Content } from "enonic-types/content";
+ import { get, Content } from "/lib/xp/content";
- export function post(req: Request): Response {
+ export function post(req: XP.Request): XP.Response {
const content: Content = get({
key: req.params.key!
})
...
}
As you can see, this will clean up imports a bit, and logically group functions and their types together.
From now on you will never do any import ... from "enonic-types/..."
directly in your code!
The one exception to this is the import from "enonic-types/libs"
to configure __non_webpack_require__
(but I recommend using JavaScript modules import
keyword instead of __non_webpack_require__
).
Protip: In the upcoming TypeScript 4.5 release we will be able to use type
modifiers on import names like this:
import { get, type Content } from "/lib/xp/content";
New features
- A new global
XP.Controller
type has been added. You can use it if you prefer arrow functions for your controllers. It can be used like this:
- import { Request, Response } from "enonic-types/controller";
- export const get = (req: Request): Response => {
+ export const get: XP.Controller = (req) => {
}