Turn your content types into strongly typed models
Improve your development experience by using strongly typed models based on your Kontent.ai content types.
Life without strongly typed models
When getting content directly from the Delivery REST API, you receive content items as JSON objects. Your app then needs to parse the JSON to display your content. For example, to access the Headline element in a content item, you'd use a notation like thisresponse.data.item.elements.headline.value
.
Here is an example of retrieving, parsing, and displaying a content item.
Use strongly typed models
This practice has several advantages:- type safety during compile-time
- convenience (IntelliSense remembers content type properties for you)
- support of type-dependent functionalities
Here is the Homepage content type used in the previous TypeScript code example.
- Each property is mapped to its content element's codename either explicitly or using a naming convention. For example, codename
body_text
becomes propertybodyText
. - Properties are usually typed using classes provided by the SDK.
- Depending on the SDK, simple content elements (like text and number) are sometimes represented using native types (like string and integer).
Generate models
We recommend that you generate the models using the TypeScript model generator.
Get strongly typed content
Sign in with your Kontent.ai credentials or sign up for free to unlock the full lesson, track your progress, and access exclusive expert insights and tips!