-
Notifications
You must be signed in to change notification settings - Fork 278
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
Add type strength to Field and getFieldValue #157
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good, but just want to clarify this statement:
This should be backwards compatible so long as users of getFieldValue() were treating its output as T | undefined.
Is it a fair assumption that users are/were doing this? Or is there anything else that can be done to ensure this wouldn't be considered a breaking change?
@aweber1 Sorry for delay, I missed the alert. First, that is a safe assumption. Anyone not meeting that assumption already had a bug:
The current implementation returns type Second, I discovered another bug-in-hiding that may surface from this PR:
The current implementation allows this, but it shouldn't. I want to make an improvement to this PR so before moving forward. Providing a default value in the third field should change the return type from |
This last commit ensures that if you provide a default value, the return type is implicitly I imported both versions into my project for a before/after:
|
@maslade I have a question regarding this PR. I have a component rendering (Angular) containing some 'LinkField' fields. I'm not able to extract the link fields using the getFieldValue function, since it requires the extracted field to be of type 'GenericFieldValue'.
Field -> defaults to GenericFieldValue which doesn't match the type of LinkField Am I doing something of the hook here or is this simply unsupported? |
Hey @evtk - sorry for not seeing this sooner. I hope this isn't too late to be helpful. The map you pass in (first parameter) supports any The type you get back is fully determined by
So in your case, |
hi @maslade thanks for checking in. I guess I have missed your response too :). I can not get this fixed, based on your suggestion. to elaborate on my specific case: the component in question receives an Input binding containing 'content'. This content is not of a type 'ComponentRendering' but contains a collection of 'ComponentFields'. Now I want to abstract the value of the href. I should use the getFieldValue for this instead of doing something like: this.content.my_Link?.value?.href :) Now since I don't have a rendering, but the fields from the rendering, I can't pass in a rendering, but I should be able to pass in this:
as the getFieldValue suggests:
so that is what I have tried in the above example |
This change makes two changes that improve typing around
Field.value
and the return value ofgetFieldValue()
. This should be backwards compatible so long as users ofgetFieldValue<T>()
were treating its output asT | undefined
.Description
Field
asField<T = GenericFieldValue>
, whereT
refers to the type of the.value
property in the field. The original value type is maintained asGenericFieldValue
, so this change is backwards compatible.getFieldValue<T>(...)
to use this new type parameter inField
to produce predictably typed results. Thus,getFieldValue<string>
will return astring | undefined
instead ofany
.Motivation
#156
How Has This Been Tested?
I modified the code locally and used the following snippet in a TypeScript enabled editor to confirm that the implied type of
value
isstring | undefined
. Repeated withboolean
,{}
, andMyClass
.Types of changes
Checklist: