Signatures of intersection/union types properties
See original GitHub issueConsider 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:
- Created 7 years ago
- Reactions:1
- Comments:6 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
We’re continuing to look for a good algorithm that can produce “correct” signatures from intersection types.
Hey all - I just wanted to add a simplified example of where this is currently failing for me as well.
https://www.typescriptlang.org/play/index.html#code/C4TwDgpgBAYgdlAvFAFCghgLigZ2AJwEs4BzASiQD5cDjyoAyVDbOAVwFsAjCfCxau268KAelFQA8m3y4QeCByhs4AE15RgEPAChQkKAAkI6VQEF8JADxnqyM1AgAPLWpyp82YgDMNAJQAaKAA6UPxtYGx0OBAAbQBdfmpokCgAfig-KFYIADcNKHEoAHF8dC5NAAtob0J8PCh0S04IOGAdPXBoACEkIxNzSyt4aiLJOAAbVKEefCCuNmBcSoB7NgnVKB4oACI8IlIoAB8oGd4dzoMAYT6-CGAZOAAVLuG4UYlxqdPOWfnF5ZrDZbaB7WiHE5nfAXHRFboA6rhWAIQjuPCECYTRpcCbQYArEFQADG6ExEE2AHdCMBKlsVjSaAcSI01D9hPgdOoiRMmtAiSs4A1vNh4FAOt4UAByaqYlaSsg6CUARgVQA