-
-
Notifications
You must be signed in to change notification settings - Fork 106
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
Fix input type on some methods and add AssertingValidator
#224
Conversation
@sindresorhus Input? |
While this isn't resolved, you can use const is = <T>(v: unknown, pred: BasePredicate<T>): v is T => ow.isValid(v, pred); to substitute |
source/index.ts
Outdated
} | ||
export type AssertingValidator<T> = T extends ReusableValidator<infer R> ? (value: unknown, label?: string) => asserts value is R : never; |
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.
Shouldn't the asserts
be added to ReusableValidator
?
source/index.ts
Outdated
}); | ||
``` | ||
*/ | ||
export type ShapeOfType<T> = Infer<DeepShape<T>>; |
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.
Why not improve the exactShape
type instead of adding a separate type here?
For some reason, I cannot reply to the review, so here:
I tried that, but then you get
First of all, I didn't want to cause breaking changes, and second, I don't know if it is possible to do that, and if it is how. As I said, I introduced that type to make sure TypeScript actually verifies, that the shape of the argument and the type of what I want to verify against are the same, since simply declaring the target variable as |
I would try to investigate that. Maybe there's a workaround. I don't have time to look into these issues now.
I unfortunately don't have time to look for a better solution. I suggest removing the unrelated changes for now and focus on "Some functions incorrectly used value: T instead of value: unknown (want to assert value is T, not assume it)". The other things can be done later on in a separate pull request or opened as an issue instead if you cannot find a good solution for it. |
I don't think it's possible due to the nature of
Understood, I'll remove them in a sec |
|
Is it? In my opinion, it should be exported, so that the user can have a validator, that actually asserts |
The intention for that was not clear. Then it needs a proper doc comment with a description (including why it exists) and a usage example. |
value: unkown
, convenience typesAssertingValidator
Fixes #222
value: T
instead ofvalue: unknown
(want toassert value is T
, not assume it)ReusableValidator
alsoassert value is T
BasePredicate
forobject.partialShape
(type-check that type and predicate are equal)I am not fully sure about the last two points, suggestions welcome.