-
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
Allow conditional check on generic parameters #51385
Comments
Duplicate #42388 |
@RyanCavanaugh I don't think #42388 is the same. The only thing it has in common is use of the |
For what it's worth, Turing complete constraints sound awesome on the surface, but in practice I think they will end up having limited appeal. This works great in first-order situations where the compiler can directly evaluate the conditional type in the function foo<T, U> where Equals<T, U>(x: T, y: U) {
// ...
}
function bar<T, U> where Equals<T, U>(x: T, u: U) {
foo(x, y); // error, distinct type parameters never compare equal
} And that's assuming the conditional type isn't just completely deferred, in which case the only safe thing for the compiler to do is to reject the call. |
@RyanCavanaugh I've seen this question and I don't think it's quite the same as my suggestion. @fatcerberus You're right. |
@lsby Adding an |
@fatcerberus Oh! You are right! In the case of generic functions calling other generic functions, denying the call is really the only safe way. type IsLT5<A> = ...
type IsLT10<A> = ...
function f1<A extends number> where [IsLT5<A>](x: A) {
// ...
}
function f2<A extends number> where [IsLT10<A>](x: A) {
f1(x)
} Suppose we have two types of calculations, For This is really unsatisfactory, but difficult to achieve by narrowing down the generic type with the A possible method is to analyze the calling process of the function, type IsLT5<A> = ...
type IsLT10<A> = ...
function f1<A extends number> where [IsLT5<A>](x: A) {
// ...
}
function f2<A extends number> where [IsLT10<A>, IsLT5<A>](x: A) {
f1(x)
} But this seems to complicate the implementation. I think the easiest way is to not allow the value of the generic type to call a function with a type IsLT5<A> = ...
type IsLT10<A> = ...
function f1<A extends number> where [IsLT10<A>](x: A) {
// ...
}
function f2<A extends number> where [IsLT5<A>](x: A) {
f1(x as any)
} In this case, But it is difficult for TS to know this. This may not be elegant, but I think it's useful, |
Suggestion
🔍 Search Terms
List of keywords you searched for before creating this issue. Write them down here so that others can find this suggestion more easily and help provide feedback.
✅ Viability Checklist
My suggestion meets these guidelines:
⭐ Suggestion
Allow conditional constraints on generic parameters.
📃 Motivating Example
This describes a function whose two arguments must be of the same type.
The
where
field provides an list, each item of this list is a type calculation, and the function can only be called if each item istrue
.where
are satisfied. So this won't affect the existing generic parameter logic.💻 Use Cases
I think this provides two benefits:
For example, there is a function that expects to enter a phone number:
For example, a function takes two arguments, both of which are items in a given list, and the arguments cannot be duplicates:
My workaround now is to do a type check on the return value of the function and return
never
if it doesn't match the condition:But this has many problems.
First, the return value is inferred as a conditional type and I need to convert it manually:
Second, when the wrong parameter is entered, the type of the return value is
never
, which is different from reporting an error.Although most of the time, when I try to use a value of type
never
, I get an error.The text was updated successfully, but these errors were encountered: