Accept de-structured elements in type predicates
See original GitHub issueSearch Terms
- type predicate
- reference binding pattern
- type predicate cannot reference
- destructured
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.:
type Example = {
a: number;
b: string | undefined;
};
const example: Example = {
a: 42,
b: 'hello';
};
of(example).pipe(
guard(({ b }): b is string => b !== undefined, 'b cannot be undefined'),
tap({ b }) => { /* b is now a string rather than a string | undefined })
);
Right now the alternative is
of(example).pipe(
guard((x): x is Omit<typeof x, 'b'> & { b: string } => x.b !== undefined, 'b cannot be undefined'),
tap({ b }) => { /* b is now a string rather than a string | undefined })
);
Or, without a predicate
of(example).pipe(
map(x => {
if (x.b === undefined) {
throw new Error();
}
return x;
}),
tap({ b }) => { /* b is now a string rather than a string | undefined })
);
Examples
function assertSomething(
{ property }: T
): property is AssertionHere {
return true;
}
This would roughly translate to something like:
function assertSomething(
obj: T
): obj is Omit<T, 'property'> & { property: AssertionHere } {
return true;
}
Checklist
My suggestion meets these guidelines:
- This wouldn’t be a breaking change in existing TypeScript/JavaScript code
- This wouldn’t change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript’s Design Goals.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:82
- Comments:11 (1 by maintainers)
Top Results From Across the Web
TS will not infer possible undefined when destructuring empty ...
When looking at the inferred type of destructured element, it will assume the array is never empty. const x: number[] = []; const...
Read more >10. Destructuring - Exploring JS
Destructuring is a convenient way of extracting multiple values from data stored in (possibly nested) objects and Arrays. It can be used in...
Read more >Typing Destructured Object Parameters in TypeScript
That way, your code doesn't compile when you attempt to call the function with an argument of an incompatible type, such as number...
Read more >TypeScript Type Guards and Type Predicates
Union types enable us to accept parameters of multiple, different types. Provide either type x or y . Sometimes, these types aren't 100% ......
Read more >Destructuring and parameter handling in ECMAScript 6 - 2ality
In locations that receive data (such as the left-hand side of an assignment), destructuring lets you use patterns to extract parts of that ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I run into this when filtering the output of
Object.entries
. I find it much more readable to be able to referencekey
andvalue
instead ofpair[0]
andpair[1]
. Simplified example but demonstrates a use case outside of rxjs.If I want all pairs from the query params that are arrays, I currently have to do:
or
I would prefer to do:
I agree that this is a useful feature and an unfortunate oversight on typescript’s part. I would, however, note that @rraziel’s workaround example can be slightly better versed by not causing an additional loop with
map
and using destructuring in thefilter
body to preserve readability.Same goes for array destructuring(my use case which involved rxjs’s
combineLatest
)