Skip to content
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

Proof of concept for using the keyof operator instead of a function parser #486

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

TimVevida
Copy link

Calls to .ensure() allowed both a string and a function as argument. The function was never actually executed, but parsed to get the string property name. The same effect can be achieved by using the keyof operator. In this case, only strings can be provided that are a property of the given class.

Proof of concept, requiring the programmer to explicitly state the object class each time .ensure() is executed. This is because .ensure() is a static function, so the type cannot be determined yet.

Fixes #485.

TimVevida added 3 commits April 20, 2018 12:14
the keyof operator.

Calls to .ensure() allowed both a string and a function as argument. The
function was never actually executed, but parsed to get the string
property name. The same effect can be achieved by using the keyof
operator. In this case, only strings can be provided that are a property
of the given class.

Initial version, requiring the programmer to explicitly state the object
class each time .ensure() is executed. This is because .ensure() is a
static function, so the type cannot be determined yet.

Fixes aurelia#485.
the keyof operator.

Calls to .ensure() allowed both a string and a function as argument. The
function was never actually executed, but parsed to get the string
property name. The same effect can be achieved by using the keyof
operator. In this case, only strings can be provided that are a property
of the given class.

Initial version, requiring the programmer to explicitly state the object
class each time .ensure() is executed. This is because .ensure() is a
static function, so the type cannot be determined yet.

Fixes aurelia#485.
@CLAassistant
Copy link

CLAassistant commented Apr 20, 2018

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


TimVevida seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@TimVevida
Copy link
Author

Note that this is a breaking change, the Aurelia Validation docs state:

The ensure method accepts one argument representing the property name. The argument can be a string or a simple property access expression. If you're using TypeScript you'll probably want to use a property access expression because you'll get the benefit of intellisense, refactoring and avoid using "magic strings" that can be a maintenance issue.

So the expression syntax is encouraged. This patch would change this and allow only string property names, which are then checked using the keyof operator, so "benefit of intellisense and refactoring" is preserved. But nevertheless a breaking change.

@jdanyow
Copy link
Contributor

jdanyow commented Apr 21, 2018

@TimVevida awesome!

Would it be feasible to ship property: keyof TObject | PropertyAccessor<TObject, TValue> first, then deprecate the prop accessor option in a future release? If possible it would be good to give folks a chance to incrementally migrate over to this nicer syntax.

@TimVevida
Copy link
Author

That is a great idea.

@jdanyow
Copy link
Contributor

jdanyow commented Apr 28, 2018

cool- any chance you could amend the PR with that change?

@RomkeVdMeulen
Copy link
Contributor

RomkeVdMeulen commented Jun 5, 2018

I think using a string rather than a function for specifying the property is a great idea. However it should be noted that you loose a little bit of functionality: while the string value is checked by the transpiler against the list of properties on your object, the value is not updated if you use automatic refactoring to rename the property. When using a function, the rename can be automatically applied.

Perhaps the recommendation should be switched from function to string, but the function syntax not be deprecated?

EDIT: See also microsoft/TypeScript#11997

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Simplify ensure() by using the keyof operator
4 participants