Accept de-structured elements in type predicates
## Search 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.: ```typescript 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 ```typescript 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 ```typescript 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 ```typescript function assertSomething( { property }: T ): property is AssertionHere { return true; } ``` This would roughly translate to something like: ```typescript function assertSomething( obj: T ): obj is Omit<T, 'property'> & { prop
Posted by rraziel on Oct 20, 2020, 9:41 AM
Tags
Related issues
- struct's lifetime requirements can be bypassed via covariant field assignment
- Proposal to Reorganize the Moderation Team and Review Moderation Policies
- Progressive FPS degradation appears to be related to dota2.com — blocking the domain prevents the performance loss
- proposal: container/...: generic collection types
- Better Custom Endpoint BYOK
- A concrete policy for backward compatibility until Godot 5.0
Raise real-world problems, find people facing the same issue, and discuss solutions.3,000 issues0 comments
github (Other)
struct's lifetime requirements can be bypassed via covariant field assignment
Proposal to Reorganize the Moderation Team and Review Moderation Policies
Progressive FPS degradation appears to be related to dota2.com — blocking the domain prevents the performance loss
proposal: container/...: generic collection types
Better Custom Endpoint BYOK
A concrete policy for backward compatibility until Godot 5.0
New MTP models not supported
Tracking: 429 / Capacity Issues
Accept de-structured elements in type predicates
## Search 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.: ```typescript 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 ```typescript 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 ```typescript 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 ```typescript function assertSomething( { property }: T ): property is AssertionHere { return true; } ``` This would roughly translate to something like: ```typescript function assertSomething( obj: T ): obj is Omit<T, 'property'> & { prop
Comments (0)
No comments yet. Start the discussion.