Add better typing capabilities #823
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
If merged, will lead to a minor release as it should be backwards compatible
feat(formal):
Rationale
Why is this PR necessary?
This PR adds type annotations so that formal can infer better types for
createRuleand all other related functions.It does not introduce mandatory types in new places ( unless you are manually instantiating
SuccessorFailclasses, which you probably aren't )these annotations can lead to typescript helping you out a lot more as you create custom rules, and in some cases, not have to run to see if things failed when you're using pipeValidators or validateObject.
They still fall back to very forgiving types that are effectively the same as before ( when most types were casted to any without the ability to specify ).
consider the examples below, pulled from the tests:
because the condition is marked with a param type, this rule will now correctly infer if you're trying to use it on some known other type by mistake, without having to wait until a failure at runtime to be certain.
it still works without complaint the way it did before if the annotation is gone. ( the same augmentation has happened with the optional transform fn )
Same effect with
withMessage.Since rules cascade over a single value with a single type, you can now put the type into the
Validatorclass and thepipeValidatorsutil.with these optional type annotations ( they all default to any ) you can get better guarantees of what will happen next with the return from this validator.
Rome has been added as a formatter / linter since it is much lighter and much faster than the eslint rules were. Any CI dealing with this package should be faster on the order of 40-60x. It makes for a messy diff where strings are getting their quotes changed, but it makes linting a whole lot easier and closer to the more standard style of prettier. it's abstracted behind the
lintandstylecommands. Because the eslint dependencies are gone, the installation should take a lot less time and the lockfile is shorter.Rome has a pretty nice VS code extension as well if you're developing and want it to format on save.
I have used pnpm as a package manager ( which I highly suggest switching to ) but have also updated yarn.lock for compatibility with CI.