https://kotlinlang.org logo
#javascript
Title
# javascript
a

Ayfri

08/22/2022, 4:54 AM
Hey I'm asking myself if it would be possible to generate using for example KSP a Partial version of any type using an annotation so we can more easily mimic the TypeScript definition that uses
Partial<something>
? It would set all the external fields to nullable and override ones from the parents.
So like it would create an external interface with the same name of the object and a prefix
Partial
, we could change the prefix or the resulting name using arguments, I've never used KSP before so this is why I'm asking this. If someone is able to try creating that, then it is also a request :)
g

Grégory Lureau

08/22/2022, 8:57 AM
KSP will only generate more code => it can take a class and wrap it so every fields are optional (with a little bit of js magic I think it's doable), or it could write another class if it's just data structure (a data class without methods for example, as KSP cannot copy-paste method content easily). Note that the class will have to be used explicitly at some point. What's the use case exactly? (if you can write a chunk of code in Kotlin even if that don't compile now)
a

Ayfri

08/22/2022, 11:13 AM
As I said it could help us use in Kotlin external typings TypeScript's
Partial<T>
type, instead of creating a similar interface of the T type but with all fields nullable, which is the more viable solution for now
l

Lukáš Kúšik

08/22/2022, 11:18 AM
I was thinking about exactly this as well, Partial types would be great for update calls (you specify only the fields you want to update). One thing to think about is, that making fields nullable would not work for those that are nullable originally. You would have to use a different type due to that, something like Arrow's Optional.
a

Ayfri

08/22/2022, 11:31 AM
I think keeping it nullable works, this is what I use for optional TypeScript fields in my external typings
l

Lukáš Kúšik

08/22/2022, 11:33 AM
Let's say a nullable is a valid value and you use Partials to update an object on the backend from something to null. How would you know whether you want to set the field to null or leave it be?
a

Ayfri

08/22/2022, 11:37 AM
I guess it would just set the field to null, as in JS optional chaining and coalescing operator verify if the value isn't
undefined
and
null
, so it's already a standard in JS that optional fields "could" be null
l

Lukáš Kúšik

08/22/2022, 11:40 AM
okay, if undefined and null are different concepts in javascript then it would be fine, I was thinking about the other Kotlin platforms 😄