-
-
Notifications
You must be signed in to change notification settings - Fork 75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support strict:true (#154), AsObject type, add clear_ methods, fix map default values #157
Conversation
Removed RecursivePartial<T>, instead introduced AsObjectPartial, which is used as fromObject method argument. Make getters, setters and AsObject typed according to canHaveNullValue predicate. Brought AsObjectPartial into line with constructor's declaration.
Change method access in RPC from property access to element access.
Add non-null assertion operators in places where an indexed access occurs.
return data && message when fromObject has no field assignments.
Oh, and there is an ambiguous change, previously discussed here The @thesayyn , We need your decision. If the change needs to be reverted, I will revert a couple of commits. |
Hey. I am swarmed this week but I'll try to take a proper look. EDIT: it would help me a lot if you could split each of these changes into their own PR. |
No problem, I will split the PR. |
I have not figured out a way to make new PRs independent, cos there are merge conflicts in generated files (maybe we should add them to gitignore). So PRs need to be merged in the following order:
|
Closing this PR, it duplicates ones listed in the comment above |
In short:
clear_*
methodstoObject
not returning a default value for map fieldsWhat exactly changed.
toObject
method now returnsAsObject
type declared inside a message namespace.fromObject
now acceptsAsObjectPartial
type, that has as many nullable fields as possible, in contrast toAsObject
.These types are useful when dealing with plain objects, which is common in frontend applications.
The
data
argument offromObject
function became nullable, so it matches the constructor declaration.The
fromObject
and constructor arguments are consistent in terms of nullability. To determine, if a field is nullable, the following function is used:This also makes repeated and map fields nullable in constructor (they weren't).
The return type of the
toObject
method (AsObject
) determines if the field is nullable by using this function:These functions have been derived after some discussion in #146 (link to the comment).
toObject
returns default values for maps (there was a bug #146 (comment))Getter and setter types changed, but this will only affect the
strict:true
users of typescript.The function above is also responsible for getter and setter type nullability.
Types of getters and setters match exactly. This is ok, cos when a field always has value (
!canHaveNullValue
), assigningundefined to it makes no sense: the getter of the field will always return some non-null value, so the code bellow will print
0
.The implication is that you cannot assign undefined to a field, that always returns non-null value (if you are using
strict:true
). This is a problem when dealing withoptional
fields, soclear_*
methods were introduced. The plugin generates them for all fields, that hadhas_*
method generated. Those methods are described in the official doc https://github.com/protocolbuffers/protobuf/blob/main/docs/field_presence.mdTsconfig options
strict
,noFallthroughCasesInSwitch
,noImplicitReturns
,noPropertyAccessFromIndexSignature
,noUncheckedIndexedAccess
,noUnusedParameters
are enabled in all tests.Options
noFallthroughCasesInSwitch
andnoImplicitReturns
required no changes.Option
noPropertyAccessFromIndexSignature
required to change method access in RPC from property access to element access.Option
noUncheckedIndexedAccess
required to add non-null assertion operator (!
) when accessing elements of#one_of_decls
, methods mentioned above, and when returningcases[case]
in oneof method. Tests were also adjusted.Option
noUnusedParameters
was covered by if statement, checking nullabledata
argument. Tests were also adjusted.I skipped some previously planned options.
Option
exactOptionalPropertyTypes
is supported only by Typescript >= 4.4, so it was skipped.Option
noUnusedLocals
conflicts with interfaces, declared for RPC, so it was skipped too.