Susmitha Gudapati
05/30/2022, 1:16 PM?
in states.
For example: My contract in ts is -
export interface A {
id: number,
name: string,
imageId?: number,
modified?: string,
}
In the interface A, the imageId and modified properties are optional. How do we denote the same functionality in kotlin’s classes?Umar Ata
05/30/2022, 1:28 PMSusmitha Gudapati
05/30/2022, 1:50 PMRoukanken
05/30/2022, 1:58 PMRoukanken
05/30/2022, 2:08 PMproperty?: type
in interface is same as if you said property: type | undefined
so if you had a Kotlin
data class A(
id: Int,
name: String,
imageId: Int? = null,
modified: String? = null,
)
then it's constructor would behave same as constructing ts/js objects in interface A
eg, equivalent of
const a: A = {
id: 1,
name: "The Answer",
imageId: 42,
}
would be
val a = A(
id = 4,
name = "The Answer",
imageId = 42,
)
(and yes, modified
would be null
and not undefined
, but unless you do some heavy magic, you won't be able to distinguish the two... but your TS code also shouldn't make difference between null & undefined)Susmitha Gudapati
05/30/2022, 2:25 PM