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.

Unions and intersections of type predicates produce wrong type

See original GitHub issue

TypeScript Version: 2.5.0-dev.20170803

Code

type Foo = typeof ts.isDoStatement | typeof ts.isWhileStatement;

Expected behavior: Resulting type should be (node: ts.Node) => node is (ts.DoStatement | ts.WhileStatement)

Actual behavior: Resulting type is (node: ts.Node) => node is ts.DoStatement

It seems to just take the first one.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:4
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
SamPrudencommented, Aug 12, 2017

type predicate

I knew using “type guards” like that wasn’t quite right. 😂

2reactions
aluanhaddadcommented, Aug 12, 2017

It appears to just grab the first type predicate and ignore the rest.

Here is a self-contained repro (2.5.0-dev.20170808)

type IsStringOrNumber = ((x: any) => x is number) | ((x: any) => x is string);

declare const x: any;

if ((((x: any) => true) as IsStringOrNumber)(x)) {
    x.toFixed();
}

Intersecting type predicates seems to have the same result, only the first signature is considered.

type IsStringAndNumber = ((x: any) => x is number) & ((x: any) => x is string);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Handbook - Unions and Intersection Types - TypeScript
Intersection and Union types are one of the ways in which you can compose types.
Read more >
Typescript type predicates not differentiating between ...
But I'm facing an issue where each predicate catches both types when applied to a union type. Here is an example: Playground Link....
Read more >
Union and intersection contracts are hard, actually - Tweag
But contracts are not exactly predicates, and this subtle difference makes the implementation of general unions ( or ) and intersections ...
Read more >
Narrowing 'this' type with multiple type predicates makes ...
Looks like unions need to include private/protected members in their property set if every union element contains them.
Read more >
Chapter 4: Union and Intersection Types - HackMD
Let's first create a TechEventBase type that contains all properties that are the same in all three event types. type TechEventBase = {...
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