Reputation: 67260
In Typescript I can declare a limited set of strings as a type:
declare type Status = 'GOOD' | 'MEDIUM' | 'POOR';
and I can then use this type to restrict the strings assigned to the status
property:
interface Foo {
status: Status;
}
How do I do that in Kotlin?
Upvotes: 4
Views: 1132
Reputation: 81889
In Kotlin, you would create an enum
for this
enum class Status {
GOOD, MEDIUM, POOR
}
In this basic case, you can then use the enum
constant's name
as follows:
val state: Status = Status.MEDIUM
val stateString: String = state.name
If you want to have more sophisticated enums, you can give them custom properties:
enum class Status(val description: String) {
GOOD("Good State"), MEDIUM("Medium State"), POOR("Poor State")
}
Upvotes: 7