JS plain objects compiler plugin
The JavaScript (JS) plain objects compiler plugin (js-plain-objects
) lets you create and copy plain JS objects in a type-safe way.
Here you can find information about plain JS objects and how to use the js-plain-objects
compiler plugin for your Kotlin/JS projects.
Plain JS objects
A plain object is a simple JS object created via an object literal ({}
) that contains data properties. Many JS APIs accept/return Plain JS objects for configuration or data exchange.
With the js-plain-objects
plugin, you declare a Kotlin external interface to describe the object shape and annotate it with @JsPlainObject
. The compiler then generates convenient functions to build and copy such objects while preserving Kotlin type safety.
Enable the plugin
Add the js-plain-objects
plugin to your project's Gradle configuration file, as the following Kotlin DSL shows:
Declare a plain object type
Once you have enabled the js-plain-objects
plugin, you can declare a plain object type. Annotate an external interface with @JsPlainObject
. For example:
When the plugin processes such an interface, it generates a companion object with two helper functions for creating and copying objects:
From the previous example:
name
andage
are declared without a nullability mark, so they are required.email
is declared as nullable, so it's optional and can be skipped during creation.The operator
invoke
builds a new plain JS object with the provided properties.The
copy
function creates a new object by shallow-copyingsource
and overriding any specified properties.The companion is marked with
@JsExport.Ignore
to avoid leaking these helpers into JS exports.
Use plain objects
Create and copy objects using the generated helpers:
The Kotlin code compiles to JavaScript:
Any JavaScript objects created with this approach are safe. When you use a wrong property name or value type, you encounter compile-time errors. This approach is also zero-cost since the generated code is inlined as a simple object literal and Object.assign
calls.
What's next
Learn more about interoperability with JavaScript in the Use JavaScript code from Kotlin and dynamic type documentation.