Skip to content

Andarist fix union intersection setters #56915

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11979,7 +11979,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (!links.writeType && links.deferralWriteConstituents) {
Debug.assertIsDefined(links.deferralParent);
Debug.assertIsDefined(links.deferralConstituents);
links.writeType = links.deferralParent.flags & TypeFlags.Union ? getUnionType(links.deferralWriteConstituents) : getIntersectionType(links.deferralWriteConstituents);
links.writeType = links.deferralParent.flags & TypeFlags.Union ? getIntersectionType(links.deferralWriteConstituents) : getUnionType(links.deferralWriteConstituents);
}
return links.writeType;
}
Expand Down Expand Up @@ -14674,7 +14674,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
else {
result.links.type = isUnion ? getUnionType(propTypes) : getIntersectionType(propTypes);
if (writeTypes) {
result.links.writeType = isUnion ? getUnionType(writeTypes) : getIntersectionType(writeTypes);
result.links.writeType = isUnion ? getIntersectionType(writeTypes) : getUnionType(writeTypes);
}
}
return result;
Expand Down
64 changes: 64 additions & 0 deletions tests/baselines/reference/divergentAccessorsTypes-56894-a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//// [tests/cases/compiler/-da/divergentAccessorsTypes-56894-a.ts] ////

//// [divergentAccessorsTypes-56894-a.ts]
declare const id:<T>()=>T

function f2(x: {f:((a:1|2)=>1|2)} | {f:((a:2|3)=>2|3)} ){
const r = x.f(id())
// ^ const id: <2>() => 2,
// ^ const r: 1 | 2 | 3
}

function f3(f: ((a:1|2)=>1|2) | ((a:2|3)=>2|3) ){
const r = f(id())
// ^ const id: <2>() => 2,
// ^ const r: 1 | 2 | 3
}


function f4(x: {set d(a:1|2); get r():1|2} | {set d(a:2|3); get r():2|3} ){
// ^ (parameter) x: { d: 1 | 2; readonly r: 1 | 2; } | { d: 2 | 3; readonly r: 2 | 3; }
x.d = id();
// ^ const id: <1 | 2 | 3>() => 1 | 2 | 3 (5.3.2)
// ^ const id: <2>() => 2 (expecting, if a setter was treated as a function)
x.r;
// ^ (property) r: 1 | 2 | 3
}

//// [divergentAccessorsTypes-56894-a.js]
"use strict";
function f2(x) {
const r = x.f(id());
// ^ const id: <2>() => 2,
// ^ const r: 1 | 2 | 3
}
function f3(f) {
const r = f(id());
// ^ const id: <2>() => 2,
// ^ const r: 1 | 2 | 3
}
function f4(x) {
// ^ (parameter) x: { d: 1 | 2; readonly r: 1 | 2; } | { d: 2 | 3; readonly r: 2 | 3; }
x.d = id();
// ^ const id: <1 | 2 | 3>() => 1 | 2 | 3 (5.3.2)
// ^ const id: <2>() => 2 (expecting, if a setter was treated as a function)
x.r;
// ^ (property) r: 1 | 2 | 3
}


//// [divergentAccessorsTypes-56894-a.d.ts]
declare const id: <T>() => T;
declare function f2(x: {
f: ((a: 1 | 2) => 1 | 2);
} | {
f: ((a: 2 | 3) => 2 | 3);
}): void;
declare function f3(f: ((a: 1 | 2) => 1 | 2) | ((a: 2 | 3) => 2 | 3)): void;
declare function f4(x: {
set d(a: 1 | 2);
get r(): 1 | 2;
} | {
set d(a: 2 | 3);
get r(): 2 | 3;
}): void;
69 changes: 69 additions & 0 deletions tests/baselines/reference/divergentAccessorsTypes-56894-a.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//// [tests/cases/compiler/-da/divergentAccessorsTypes-56894-a.ts] ////

=== divergentAccessorsTypes-56894-a.ts ===
declare const id:<T>()=>T
>id : Symbol(id, Decl(divergentAccessorsTypes-56894-a.ts, 0, 13))
>T : Symbol(T, Decl(divergentAccessorsTypes-56894-a.ts, 0, 18))
>T : Symbol(T, Decl(divergentAccessorsTypes-56894-a.ts, 0, 18))

function f2(x: {f:((a:1|2)=>1|2)} | {f:((a:2|3)=>2|3)} ){
>f2 : Symbol(f2, Decl(divergentAccessorsTypes-56894-a.ts, 0, 25))
>x : Symbol(x, Decl(divergentAccessorsTypes-56894-a.ts, 2, 12))
>f : Symbol(f, Decl(divergentAccessorsTypes-56894-a.ts, 2, 16))
>a : Symbol(a, Decl(divergentAccessorsTypes-56894-a.ts, 2, 20))
>f : Symbol(f, Decl(divergentAccessorsTypes-56894-a.ts, 2, 37))
>a : Symbol(a, Decl(divergentAccessorsTypes-56894-a.ts, 2, 41))

const r = x.f(id())
>r : Symbol(r, Decl(divergentAccessorsTypes-56894-a.ts, 3, 9))
>x.f : Symbol(f, Decl(divergentAccessorsTypes-56894-a.ts, 2, 16), Decl(divergentAccessorsTypes-56894-a.ts, 2, 37))
>x : Symbol(x, Decl(divergentAccessorsTypes-56894-a.ts, 2, 12))
>f : Symbol(f, Decl(divergentAccessorsTypes-56894-a.ts, 2, 16), Decl(divergentAccessorsTypes-56894-a.ts, 2, 37))
>id : Symbol(id, Decl(divergentAccessorsTypes-56894-a.ts, 0, 13))

// ^ const id: <2>() => 2,
// ^ const r: 1 | 2 | 3
}

function f3(f: ((a:1|2)=>1|2) | ((a:2|3)=>2|3) ){
>f3 : Symbol(f3, Decl(divergentAccessorsTypes-56894-a.ts, 6, 1))
>f : Symbol(f, Decl(divergentAccessorsTypes-56894-a.ts, 8, 12))
>a : Symbol(a, Decl(divergentAccessorsTypes-56894-a.ts, 8, 17))
>a : Symbol(a, Decl(divergentAccessorsTypes-56894-a.ts, 8, 34))

const r = f(id())
>r : Symbol(r, Decl(divergentAccessorsTypes-56894-a.ts, 9, 9))
>f : Symbol(f, Decl(divergentAccessorsTypes-56894-a.ts, 8, 12))
>id : Symbol(id, Decl(divergentAccessorsTypes-56894-a.ts, 0, 13))

// ^ const id: <2>() => 2,
// ^ const r: 1 | 2 | 3
}


function f4(x: {set d(a:1|2); get r():1|2} | {set d(a:2|3); get r():2|3} ){
>f4 : Symbol(f4, Decl(divergentAccessorsTypes-56894-a.ts, 12, 1))
>x : Symbol(x, Decl(divergentAccessorsTypes-56894-a.ts, 15, 12))
>d : Symbol(d, Decl(divergentAccessorsTypes-56894-a.ts, 15, 16))
>a : Symbol(a, Decl(divergentAccessorsTypes-56894-a.ts, 15, 22))
>r : Symbol(r, Decl(divergentAccessorsTypes-56894-a.ts, 15, 29))
>d : Symbol(d, Decl(divergentAccessorsTypes-56894-a.ts, 15, 46))
>a : Symbol(a, Decl(divergentAccessorsTypes-56894-a.ts, 15, 52))
>r : Symbol(r, Decl(divergentAccessorsTypes-56894-a.ts, 15, 59))

// ^ (parameter) x: { d: 1 | 2; readonly r: 1 | 2; } | { d: 2 | 3; readonly r: 2 | 3; }
x.d = id();
>x.d : Symbol(d, Decl(divergentAccessorsTypes-56894-a.ts, 15, 16), Decl(divergentAccessorsTypes-56894-a.ts, 15, 46))
>x : Symbol(x, Decl(divergentAccessorsTypes-56894-a.ts, 15, 12))
>d : Symbol(d, Decl(divergentAccessorsTypes-56894-a.ts, 15, 16), Decl(divergentAccessorsTypes-56894-a.ts, 15, 46))
>id : Symbol(id, Decl(divergentAccessorsTypes-56894-a.ts, 0, 13))

// ^ const id: <1 | 2 | 3>() => 1 | 2 | 3 (5.3.2)
// ^ const id: <2>() => 2 (expecting, if a setter was treated as a function)
x.r;
>x.r : Symbol(r, Decl(divergentAccessorsTypes-56894-a.ts, 15, 29), Decl(divergentAccessorsTypes-56894-a.ts, 15, 59))
>x : Symbol(x, Decl(divergentAccessorsTypes-56894-a.ts, 15, 12))
>r : Symbol(r, Decl(divergentAccessorsTypes-56894-a.ts, 15, 29), Decl(divergentAccessorsTypes-56894-a.ts, 15, 59))

// ^ (property) r: 1 | 2 | 3
}
73 changes: 73 additions & 0 deletions tests/baselines/reference/divergentAccessorsTypes-56894-a.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//// [tests/cases/compiler/-da/divergentAccessorsTypes-56894-a.ts] ////

=== divergentAccessorsTypes-56894-a.ts ===
declare const id:<T>()=>T
>id : <T>() => T

function f2(x: {f:((a:1|2)=>1|2)} | {f:((a:2|3)=>2|3)} ){
>f2 : (x: { f: (a: 1 | 2) => 1 | 2; } | { f: (a: 2 | 3) => 2 | 3; }) => void
>x : { f: (a: 1 | 2) => 1 | 2; } | { f: (a: 2 | 3) => 2 | 3; }
>f : (a: 1 | 2) => 1 | 2
>a : 1 | 2
>f : (a: 2 | 3) => 2 | 3
>a : 2 | 3

const r = x.f(id())
>r : 1 | 2 | 3
>x.f(id()) : 1 | 2 | 3
>x.f : ((a: 1 | 2) => 1 | 2) | ((a: 2 | 3) => 2 | 3)
>x : { f: (a: 1 | 2) => 1 | 2; } | { f: (a: 2 | 3) => 2 | 3; }
>f : ((a: 1 | 2) => 1 | 2) | ((a: 2 | 3) => 2 | 3)
>id() : 2
>id : <T>() => T

// ^ const id: <2>() => 2,
// ^ const r: 1 | 2 | 3
}

function f3(f: ((a:1|2)=>1|2) | ((a:2|3)=>2|3) ){
>f3 : (f: ((a: 1 | 2) => 1 | 2) | ((a: 2 | 3) => 2 | 3)) => void
>f : ((a: 1 | 2) => 1 | 2) | ((a: 2 | 3) => 2 | 3)
>a : 1 | 2
>a : 2 | 3

const r = f(id())
>r : 1 | 2 | 3
>f(id()) : 1 | 2 | 3
>f : ((a: 1 | 2) => 1 | 2) | ((a: 2 | 3) => 2 | 3)
>id() : 2
>id : <T>() => T

// ^ const id: <2>() => 2,
// ^ const r: 1 | 2 | 3
}


function f4(x: {set d(a:1|2); get r():1|2} | {set d(a:2|3); get r():2|3} ){
>f4 : (x: { d: 1 | 2; readonly r: 1 | 2; } | { d: 2 | 3; readonly r: 2 | 3; }) => void
>x : { d: 1 | 2; readonly r: 1 | 2; } | { d: 2 | 3; readonly r: 2 | 3; }
>d : 1 | 2
>a : 1 | 2
>r : 1 | 2
>d : 2 | 3
>a : 2 | 3
>r : 2 | 3

// ^ (parameter) x: { d: 1 | 2; readonly r: 1 | 2; } | { d: 2 | 3; readonly r: 2 | 3; }
x.d = id();
>x.d = id() : 1 | 2 | 3
>x.d : 1 | 2 | 3
>x : { d: 1 | 2; readonly r: 1 | 2; } | { d: 2 | 3; readonly r: 2 | 3; }
>d : 1 | 2 | 3
>id() : 1 | 2 | 3
>id : <T>() => T

// ^ const id: <1 | 2 | 3>() => 1 | 2 | 3 (5.3.2)
// ^ const id: <2>() => 2 (expecting, if a setter was treated as a function)
x.r;
>x.r : 1 | 2 | 3
>x : { d: 1 | 2; readonly r: 1 | 2; } | { d: 2 | 3; readonly r: 2 | 3; }
>r : 1 | 2 | 3

// ^ (property) r: 1 | 2 | 3
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
divergentAccessorsTypes-56894-b.ts(26,9): error TS2367: This comparison appears to be unintentional because the types '3' and '1' have no overlap.
divergentAccessorsTypes-56894-b.ts(34,5): error TS2322: Type '1' is not assignable to type '2'.
divergentAccessorsTypes-56894-b.ts(35,5): error TS2322: Type '1' is not assignable to type '2'.
divergentAccessorsTypes-56894-b.ts(36,5): error TS2322: Type '1' is not assignable to type '2'.
divergentAccessorsTypes-56894-b.ts(37,9): error TS2367: This comparison appears to be unintentional because the types '2' and '1' have no overlap.
divergentAccessorsTypes-56894-b.ts(40,9): error TS2367: This comparison appears to be unintentional because the types '2' and '1' have no overlap.


==== divergentAccessorsTypes-56894-b.ts (6 errors) ====
type T12 = {
a: 1|2;
set gsa(x: 1|2);
get gsa(): 1|2;
set sb(x: 1|2);
get gc(): 1|2;
};
type T23 = {
a: 2|3;
set gsa(x: 2|3);
get gsa(): 2|3;
set sb(x: 2|3);
get gc(): 2|3;
};

function fu(u:T12|T23) {
u.a = 1;
u.sb = 1;
u.gsa = 1;
u.a = 2;
u.sb = 2;
u.gsa = 2;
u.a = 3;
u.sb = 3;
u.gsa = 3;
if (u.gsa === 1) {
~~~~~~~~~~~
!!! error TS2367: This comparison appears to be unintentional because the types '3' and '1' have no overlap.
u.gsa;
}
if (u.gc === 1) {
u.gc;
}
}
function fi(i:T12&T23) {
i.a = 1; // error before & after
~~~
!!! error TS2322: Type '1' is not assignable to type '2'.
i.sb = 1; // error before & after
~~~~
!!! error TS2322: Type '1' is not assignable to type '2'.
i.gsa = 1; // error before & after
~~~~~
!!! error TS2322: Type '1' is not assignable to type '2'.
if (i.gsa === 1) { // error before & after
~~~~~~~~~~~
!!! error TS2367: This comparison appears to be unintentional because the types '2' and '1' have no overlap.
i.gsa;
}
if (i.gc === 1) { // error before & after
~~~~~~~~~~
!!! error TS2367: This comparison appears to be unintentional because the types '2' and '1' have no overlap.
i.gc;
}
}
76 changes: 76 additions & 0 deletions tests/baselines/reference/divergentAccessorsTypes-56894-b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//// [tests/cases/compiler/-da/divergentAccessorsTypes-56894-b.ts] ////

//// [divergentAccessorsTypes-56894-b.ts]
type T12 = {
a: 1|2;
set gsa(x: 1|2);
get gsa(): 1|2;
set sb(x: 1|2);
get gc(): 1|2;
};
type T23 = {
a: 2|3;
set gsa(x: 2|3);
get gsa(): 2|3;
set sb(x: 2|3);
get gc(): 2|3;
};

function fu(u:T12|T23) {
u.a = 1;
u.sb = 1;
u.gsa = 1;
u.a = 2;
u.sb = 2;
u.gsa = 2;
u.a = 3;
u.sb = 3;
u.gsa = 3;
if (u.gsa === 1) {
u.gsa;
}
if (u.gc === 1) {
u.gc;
}
}
function fi(i:T12&T23) {
i.a = 1; // error before & after
i.sb = 1; // error before & after
i.gsa = 1; // error before & after
if (i.gsa === 1) { // error before & after
i.gsa;
}
if (i.gc === 1) { // error before & after
i.gc;
}
}

//// [divergentAccessorsTypes-56894-b.js]
function fu(u) {
u.a = 1;
u.sb = 1;
u.gsa = 1;
u.a = 2;
u.sb = 2;
u.gsa = 2;
u.a = 3;
u.sb = 3;
u.gsa = 3;
if (u.gsa === 1) {
u.gsa;
}
if (u.gc === 1) {
u.gc;
}
}
function fi(i) {
i.a = 1; // error before & after
i.sb = 1; // error before & after
i.gsa = 1; // error before & after
if (i.gsa === 1) { // error before & after
i.gsa;
}
if (i.gc === 1) { // error before & after
i.gc;
}
}
Loading