microsoft / microsoft/TypeScript

Accept de-structured elements in type predicates

Open
#41,173 24 comments 181 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

  • 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.

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

No files, tests, or compiler entry points are identified in the issue. Start by reading the type-predicate and destructured-parameter examples, then trace how TypeScript currently validates predicate parameter and return types. Done means destructured parameters in type predicates are accepted and their narrowed types behave as shown without changing emitted JavaScript.

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
Quiet
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.