Default values
Some properties of a class are optional, or nullable, or both.
Default values
If a value has a default value, then it is not required for creating an encoded message. Therefore,
it will be marked as optional using the ?:
notation.
@Serializable
class Colour(val rgb: Int = 12345)
fun main() {
val tsGenerator = KxsTsGenerator()
println(tsGenerator.generate(Colour.serializer()))
}
You can get the full code here.
export interface Colour {
rgb?: number;
}
Nullable values
Properties might be required, but the value can be nullable. In TypeScript that is represented with
a type union that includes null
.
@Serializable
class Colour(val rgb: Int?) // 'rgb' is required, but the value can be null
fun main() {
val tsGenerator = KxsTsGenerator()
println(tsGenerator.generate(Colour.serializer()))
}
You can get the full code here.
export interface Colour {
rgb: number | null;
}
Default and nullable
A property can be both nullable and optional, which gives four possible options.
@Serializable
data class ContactDetails(
// nullable: ❌, optional: ❌
val name: String,
// nullable: ✅, optional: ❌
val email: String?,
// nullable: ❌, optional: ✅
val active: Boolean = true,
// nullable: ✅, optional: ✅
val phoneNumber: String? = null,
)
fun main() {
val tsGenerator = KxsTsGenerator()
println(tsGenerator.generate(ContactDetails.serializer()))
}
You can get the full code here.
export interface ContactDetails {
name: string;
email: string | null;
active?: boolean;
phoneNumber?: string | null;
}
Override optional properties
Properties with default values can be set as required using the Kotlinx Serialization annotation,
@kotlinx.serialization.Required
.
For demonstration purposes, let's see what happens when @Required
is added to all properties.
@Serializable
data class ContactDetails(
@Required
val name: String,
@Required
val email: String?,
@Required
val active: Boolean = true,
@Required
val phoneNumber: String? = null,
)
fun main() {
val tsGenerator = KxsTsGenerator()
println(tsGenerator.generate(ContactDetails.serializer()))
}
You can get the full code here.
active
and phoneNumber
are now required properties. Note that @Required
had no effect
on name
or email
; because they do not have default values, they were already required.
export interface ContactDetails {
name: string;
email: string | null;
active: boolean;
phoneNumber: string | null;
}