microsoft / microsoft/TypeScript

Typeguards with destructuring parameters

Open
#28,311 2 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

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);

https://www.typescriptlang.org/play/index.html#src=const%20val%3A%20Array%3C%5Bstring%2C%20boolean%5D%3E%20%3D%20(%5B%5D%20as%20Array%3C%5Bstring%20%7C%20undefined%2C%20boolean%5D%3E)%0D%0A%20%20%20%20.filter((%5Bfoo%2C%20bar%5D%3A%20%5Bstring%20%7C%20undefined%2C%20boolean%5D)%3A%20%5Bfoo%2C%20bar%5D%20is%20%5Bstring%2C%20boolean%5D%20%3D%3E%20foo%20!%3D%3D%20undefined)%3B%0D%0A%0D%0Aconst%20val2%3A%20Array%3C%5Bstring%2C%20boolean%5D%3E%20%3D%20(%5B%5D%20as%20Array%3C%5Bstring%20%7C%20undefined%2C%20boolean%5D%3E)%0D%0A%20%20%20%20.filter((val%3A%20%5Bstring%20%7C%20undefined%2C%20boolean%5D)%3A%20val%20is%20%5Bstring%2C%20boolean%5D%20%3D%3E%20val%5B0%5D%20!%3D%3D%20undefined)%3B%0D%0A%0D%0Aconst%20val3%3A%20string%5B%5D%20%3D%20(%5B%5D%20as%20Array%3Cstring%20%7C%20undefined%3E)%0D%0A%20%20%20%20.filter((foo%3F%3A%20string)%20%3D%3E%20foo%20!%3D%3D%20undefined)%3B%0D%0A

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. new expression-level syntax)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the destructured and non-destructured examples in the linked TypeScript Playground. The issue names no source file or test; trace how type predicates and destructured parameters are handled. Done means the destructured predicate narrows the filtered tuple type without changing emitted JavaScript, with regression coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.