question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Accept de-structured elements in type predicates

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:82
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

12reactions
manbearwizcommented, Nov 20, 2020

I run into this when filtering the output of Object.entries. I find it much more readable to be able to reference key and value instead of pair[0] and pair[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:

const queryParams = {text: 'foo', statuses: ['status1', 'status2'], regions: []}

Object.entries(queryParams)
 .filter((pair): pair is [string, string[]] => Array.isArray(pair[1]))

or

Object.entries(queryParams)
  .filter(([_, value]) => Array.isArray(value))
  .map(pair => pair as [string, string[]])

I would prefer to do:

Object.entries(queryParams )
  .filter(([_, value]): value is string[] => Array.isArray(value))
7reactions
artu-olecommented, Oct 5, 2021

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 the filter body to preserve readability.

const filtered = xs
  .filter((x): x is { value: number } => {
    const { value } = x;
    return typeof value === 'number';
  });

Same goes for array destructuring(my use case which involved rxjs’s combineLatest)

type X = [ number | string ];
const xs: Array<X> = [[ 42 ], [ 'hello' ]];

// without the feature
const filtered = xs
  .filter((x) => {
    const [ value ] = x;
    return typeof value === 'number';
  })
;

// with the feature
const filtered = xs
  .filter(([ value ]): value is number => typeof value === 'number')
;

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found