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.

Signatures of intersection/union types properties

See original GitHub issue

Consider next example:

interface A{
    f:string;
    set_f(val:string) : void;
    get_f() : string;
}

interface B{
    f:number;
    set_f(val:number) : void;
    get_f() : number;
}

let n:number;
let str:string

let first : A&B;
first.f = n; // error, as f has type number&string. Probably OK, but probably it should be number|string, to be symmetric with set_f function.
first.f = str; // error, as f has type number&string.
n = first.f;
str = first.f;

first.set_f(n);
first.set_f(str);
n = first.get_f(); // error, typescript prefer to use first signature () => string. Shouldn't it be string&number.
str = first.get_f();

let second : A|B;
second.f = n; // Ops, no error! But it should be at least string&number here
second.f = str;// Ops, no error! But it should be at least string&number here
n = second.f; // error, typescript prefer to use first signature string. It should be string|number
str = second.f;// Ops, no error, typescript prefer to use first signature string. It should be string|number

second.set_f(n); //error, lack a call signature. It's really good, but better if it would be (string&number) => void
second.set_f(str); //error, lack a call signature. 
n = second.get_f(); // error, cool, typescript was able to correctly infer result as string|number
str = second.get_f();// error, cool, typescript was able to correctly infer result as string|number

With #9167 additional to previous was added preserving readonly flag for intersection. Should’t readonly flag be reset if any of intersection member has no such flag? As intersection should extend possibilities of type, and never narrow down it.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

github_iconTop Results From Across the Web

Handbook - Unions and Intersection Types - TypeScript
A union type describes a value that can be one of several types. We use the vertical bar ( | ) to separate...
Read more >
Typescript union types are intersection types and vice versa.
An intersection type operator would return a type that has only the shared properties of the operands.
Read more >
Chapter 4: Union and Intersection Types - HackMD
We call this concept intersection types. We read the & operator as and. We combine the properties from one type A with that...
Read more >
An overview of computing with types • Tackling TypeScript
TypeScript represents collections of metavalues as unions of literal types. ... The intersection of two object types has the properties of both types:....
Read more >
Reconstructing TypeScript, part 5: intersection types
A union type A | B is a logical OR of its arms: for any value satisfying the type, we know that either...
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