-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Accept de-structured elements in type predicates #41173
Comments
Self-contained examples that don't assume importing/knowledge of rxjs would be very helpful |
I run into this when filtering the output of If I want all pairs from the query params that are arrays, I currently have to do: const queryParams = {text: 'foo', statuses: ['status1', 'status2'], regions: []}
Object.entries(queryParams)
.filter((pair): pair is [string, string[]] => Array.isArray(pair[1])) or Object.entries(queryParams)
.filter(([_, value]) => Array.isArray(value))
.map(pair => pair as [string, string[]]) I would prefer to do: Object.entries(queryParams )
.filter(([_, value]): value is string[] => Array.isArray(value)) |
A simple-with-no-external-elements example could be: type X = { value: number | string; };
const xs: Array<X> = [{ value: 42 }, { value: 'hello' }];
// without the feature
const filtered = xs
.filter(({ value }) => typeof value === 'number')
.map(x => x as { value: number })
;
// with the feature
const filtered = xs
.filter(({ value }): value is number => typeof value === 'number')
; |
Ran into this problem in React. I have a React context in the form of a class. function assertSomething(
obj: T
): obj is Omit<T, 'property'> & { property: AssertionHere } {
return true;
} in my project as suggested by @rraziel doesn't work. Turns out, since In my project, intellisense reported
In conclusion, it'll be real nice for this feature to be implemented (or this bug to be fixed). |
I agree that this is a useful feature and an unfortunate oversight on typescript's part. const filtered = xs
.filter((x): x is { value: number } => {
const { value } = x;
return typeof value === 'number';
}); Same goes for array destructuring(my use case which involved rxjs's type X = [ number | string ];
const xs: Array<X> = [[ 42 ], [ 'hello' ]];
// without the feature
const filtered = xs
.filter((x) => {
const [ value ] = x;
return typeof value === 'number';
})
;
// with the feature
const filtered = xs
.filter(([ value ]): value is number => typeof value === 'number')
; |
Since this has the Awaiting More Feedback label I'd like to add that this is an important feature request from me as well. |
+1 |
In need of this feature as well !! With the feature :
|
This would be pretty neat! Example of how I would have liked to use this (bit of a compacted example):
Explanation: |
+1 I'd like to support this suggestion, too. |
Want to support this issue as well. Here's our case: We were trying to create a predicate which shows which type of result is returned based on the boolean Thanks in advance! |
Another super simple example use case would be if we have
this works fine:
but if the types is more complex or if you are filtering by multiple keys it would be really helpful to be able to de-structure the predicate like:
|
Seriously, why is this not already a thing? The one time I want to use destructuring of props in a function and it does not work. Strong upvote. |
Any workaround for this when destructuring tuples? const rolledValues = new Map<string | undefined, number>()
filter(
rolledValues,
([key, value]): key is NonNullable<unknown> => !!key,
)
// `filter` is similar to `Array.filter` but works on any iterable. Had to write it like this, not the most convenient... (tuple): tuple is [string | number, number] => !!tuple[0] |
Me too. My use case is "Object.entries" which is already present by manbearwiz comment. |
Interesting, can it be covered by #57465 ? |
A whopping ten months after the last time this was asked, why are we still in "Awaiting More Feedback" on this one? If it's possible to do it, can we just get on and do it? If it's not possible to do it, can we explicitly state that and close this off? If it's just low priority but on the wishlist, can we explicitly state that so people can avoid being confused by the silence? |
Those are all good reminders, but the one thing that I see causing the most frustration is lack of transparency. What does the development team think about this issue (as far as desirability, feasibility, and priority) and what does it need the community to do to mature it enough that it can be fully evaluated? As for this issue in particular, this is what I see:
I understand that your time is limited, but if you are going to take the time to comment at all, the most constructive thing you could do is tell us how we can help you bring this issue to a resolution. |
This seems like a good feature, any news on this? |
Search Terms
Suggestion
The possibility to use destructured parameters in type predicates.
Use Cases
Destructuring is heavily used in functional/reactive programming, notably with rxjs where various contextual properties tend to be passed between each operator.
Having the ability to succinctly test for types would make the code more readable, e.g.:
Right now the alternative is
Or, without a predicate
Examples
This would roughly translate to something like:
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: