-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Feature Request: Custom errors via type param utility when used as a default #41392
Comments
Duplicate of #23689. |
@MartinJohns while the intention is similar, the approaches are most certainly different. This issue's proposed utility type includes type params for specifying error location and for explicitly telling whether to throw (encourages predicate reuse). The supposed duplicate's proposed utility type: type invalid<M extends string> = intrinsic; This issue's proposed utility type: type ParamError<Message extends string, Target, ShouldThrow extends boolean> = intrinsic;
// ^ ^
// | Different
// Different The greatest difference however, is that this issue describes a specific context in which to use this type (similarly to type X<
T,
TIsInvalid = ParamError<"Invalid message", T, IsValid<T>>
> = // ... This is very different from the supposed duplicate's proposed place of use. That proposed usage occurs through a conditional type, which is to then be used as the right-hand of an intersection with a type variable's constraint). I'm not the arbiter of truth or kindness in OS... but hand-waving an issue as a duplicate is not productive. I'd love feedback, positive or negative. As long as you come to it with an open mind. Thank you. |
The feature you want is the exact same: A way to provide custom errors. Only the approach and the details differ slightly. This could have been a comment to aid the discussion regarding the desired feature in a centralized place, instead of scattering it across issues. This is not "hand-waving" or "closed mindness". It's simply pointing out that this feature was already requested before. :-) |
A) "Slightly" is an understatement. @MartinJohns, I'd be curious to hear your thoughts on this design idea. Not sure if you often give feedback, or if your goals are more in line with helping keep the peace / reduce noise in TS issues. Either way, gotta love this community ;) |
I agree this is a duplicate of #23689 - we prefer to organize issues by use case / end-goal, not have competing separate issues for different syntactic representations. |
Well alright. The oracle has spoken. |
Search Terms
Throw, error, in, type, param, custom, message, assert
The Problem
The ability to signal sources of error is critical to the productivity of both library developers and consumers; without this ability, it's easy to get lost in a sea of difficult-to-trace
never
s.PR #40468 is incredibly exciting. However, it's unclear how users could specify the positioning and bubbling of error messages. This issue details a different approach, one using a would-be intrinsic
ParamError
utility type, which would be used in lieu of type variable defaults.Suggestion
In the use case below, I describe a generic
IsEmailAddress
predicate type, and anacceptsEmailAddress
function, which constrains the first param according to theIsEmailAddress
predicate. Here's how such validation would look usingParamError
:One could have multiple validations target the same type param:
Use Case
While the key use case is enabling library developers to create simpler error messaging experiences (and put aside the practice of
never
-hunting), you may have noticed another critical use case: constraining params by more than just subtype. Thanks to conditional and recursive types, we can traverse large structures and make judgements predicated upon more than could be represented with a subtype check.Let's say we want to validate that a string literal is an email address. We begin by building the
IsEmail
predicate and its children.This is not a type that we can represent today as a generic constraint. Aka, we are unable to do the following:
While I hope that conditional assignability soon solves this problem, we're currently stuck. The only solutions seem to be:
acceptsEmailAddress
's output is referenced (also difficult to maintain / enforce).Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: