Open
Description
Search Terms
Typeguard
type narrowing
destructuring
Suggestion
It'd be nice to be able to have typeguard functions with destructuring arguments.
Use Cases
When manipulating tuples or other anonymous structured types, whose typeguard relies only on a small part of the object, it would make the code much easier to read.
Currently, typeguards cannot be used with destructuring arguments, which makes it a weird special case.
Examples
const val: Array<[string, boolean]> = ([] as Array<[string | undefined, boolean]>)
.filter(([foo, bar]: [string | undefined, boolean]): [foo, bar] is [string, boolean] => foo !== undefined);
const val2: Array<[string, boolean]> = ([] as Array<[string | undefined, boolean]>)
.filter((val: [string | undefined, boolean]): val is [string, boolean] => val[0] !== undefined);
Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript / JavaScript codeThis wouldn't change the runtime behavior of existing JavaScript codeThis could be implemented without emitting different JS based on the types of the expressionsThis isn't a runtime feature (e.g. new expression-level syntax)
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
jamesblack commentedon Jul 1, 2019
I'm afraid of bumping a super old thing, but I think i'm running into something that would be resolved if this was.
Given this function
Given this callsite
The
policy.version
policy is coming back as possiby null, even though it cannot be null if validation errors is null.