Open RIP21 opened 3 years ago
I spent some good time searching for duplicates and Google through the internet and experimented myself guessing as it's supported somehow, but didn't manage to find anything similar to that. If it's a duplicate, I'm sorry :)
-set
is readonly
, which exists already.
-get
is writeonly
, which isn't a sort of type we're equipped to reason about.
Not sure what to do with this.
@RyanCavanaugh some utility types for class manipulations based on access modifiers maybe? :) There is many things I talked about in the issue :)
I have problem with ignoring getters and setters somehow generically and in bulks and not a one off using Omit.
And, well, readonly field in type extracted from class definition is just field: "value"
but for getter it's actually a function. Same for setter.
So they're not 1:1 identical as far as I understand.
Also I want to Omit fields based on private / protected and currently its impossible.
Suggestion
π Search Terms
get, set, mapped types, remove getters from type, remove setters from type, get public fields only from class
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
Being able to remove/collect keys of
get
set
types using Mapped Types syntax + many other utils to make types from class definitions based on their access level, type, return type, etc.π Motivating Example
Basically, I have classes everywhere and I want to generate for it a default constructor that is taken from
BaseClass
that takes all the fields passed and assigns them to the fields of the class.E.g.
That then gets extended.
Something like
And some "magic" type utils like
PublicFields<ClassName>
PrivateFields<ClassName
or maybe some special syntax to do such things onclass
definitions to get plaintype
will be awesome.Problem is that I cannot make type util e.g. that
DefaultConstructorFields
, that will ignore all function fields, get fields, replace set with just whatever comes as an argument to the setter.Currently, it's possible to filter functions/read-only fields/based on type/name, but not
get
andset
, or filter by field visibility such aspublic
protected
private
and ES6 private#field
. Only manualOmit
is possible.π» Use Cases
Ideally, I want to have my default constructors up and running with minimal overhead. E.g. like Kotlin has
data class
that just generates POJO with default constructor/hashCode/equals etc. while still allowing forget
for calculated properties andset
for setting things up while initializing the instance through the constructor.So my main use case is strict Model classes (typeorm in my case) with lots of annotations and GraphQL Types that are still classes, because it's how Nest.js GraphQL works, it needs a runtime class e.g. prototype to defer the type at runtime.