Skip to content

Commit 06746ef

Browse files
authored
Revert change to getFalsyFlags (microsoft#47125)
* Revert change to getFalsyFlags * Add regression test
1 parent d417058 commit 06746ef

File tree

6 files changed

+158
-1
lines changed

6 files changed

+158
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20893,7 +20893,7 @@ namespace ts {
2089320893
// flags for the string, number, boolean, "", 0, false, void, undefined, or null types respectively. Returns
2089420894
// no flags for all other types (including non-falsy literal types).
2089520895
function getFalsyFlags(type: Type): TypeFlags {
20896-
return type.flags & TypeFlags.UnionOrIntersection ? getFalsyFlagsOfTypes((type as UnionType).types) :
20896+
return type.flags & TypeFlags.Union ? getFalsyFlagsOfTypes((type as UnionType).types) :
2089720897
type.flags & TypeFlags.StringLiteral ? (type as StringLiteralType).value === "" ? TypeFlags.StringLiteral : 0 :
2089820898
type.flags & TypeFlags.NumberLiteral ? (type as NumberLiteralType).value === 0 ? TypeFlags.NumberLiteral : 0 :
2089920899
type.flags & TypeFlags.BigIntLiteral ? isZeroBigInt(type as BigIntLiteralType) ? TypeFlags.BigIntLiteral : 0 :

tests/baselines/reference/spreadObjectOrFalsy.errors.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,22 @@ tests/cases/conformance/types/spread/spreadObjectOrFalsy.ts(10,14): error TS2698
3939
...z
4040
};
4141
}
42+
43+
// Repro from #47028
44+
45+
interface DatafulFoo<T> {
46+
data: T;
47+
}
48+
49+
class Foo<T extends string> {
50+
data: T | undefined;
51+
bar() {
52+
if (this.hasData()) {
53+
this.data.toLocaleLowerCase();
54+
}
55+
}
56+
hasData(): this is DatafulFoo<T> {
57+
return true;
58+
}
59+
}
4260

tests/baselines/reference/spreadObjectOrFalsy.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,24 @@ function g1<T extends {}, A extends { z: (T | undefined) & T }>(a: A) {
3131
...z
3232
};
3333
}
34+
35+
// Repro from #47028
36+
37+
interface DatafulFoo<T> {
38+
data: T;
39+
}
40+
41+
class Foo<T extends string> {
42+
data: T | undefined;
43+
bar() {
44+
if (this.hasData()) {
45+
this.data.toLocaleLowerCase();
46+
}
47+
}
48+
hasData(): this is DatafulFoo<T> {
49+
return true;
50+
}
51+
}
3452

3553

3654
//// [spreadObjectOrFalsy.js]
@@ -69,6 +87,19 @@ function g1(a) {
6987
var z = a.z;
7088
return __assign({}, z);
7189
}
90+
var Foo = /** @class */ (function () {
91+
function Foo() {
92+
}
93+
Foo.prototype.bar = function () {
94+
if (this.hasData()) {
95+
this.data.toLocaleLowerCase();
96+
}
97+
};
98+
Foo.prototype.hasData = function () {
99+
return true;
100+
};
101+
return Foo;
102+
}());
72103

73104

74105
//// [spreadObjectOrFalsy.d.ts]
@@ -81,3 +112,11 @@ declare function f6<T extends object | undefined>(a: T): T;
81112
declare function g1<T extends {}, A extends {
82113
z: (T | undefined) & T;
83114
}>(a: A): (T | undefined) & T;
115+
interface DatafulFoo<T> {
116+
data: T;
117+
}
118+
declare class Foo<T extends string> {
119+
data: T | undefined;
120+
bar(): void;
121+
hasData(): this is DatafulFoo<T>;
122+
}

tests/baselines/reference/spreadObjectOrFalsy.symbols

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,46 @@ function g1<T extends {}, A extends { z: (T | undefined) & T }>(a: A) {
8585
};
8686
}
8787

88+
// Repro from #47028
89+
90+
interface DatafulFoo<T> {
91+
>DatafulFoo : Symbol(DatafulFoo, Decl(spreadObjectOrFalsy.ts, 31, 1))
92+
>T : Symbol(T, Decl(spreadObjectOrFalsy.ts, 35, 21))
93+
94+
data: T;
95+
>data : Symbol(DatafulFoo.data, Decl(spreadObjectOrFalsy.ts, 35, 25))
96+
>T : Symbol(T, Decl(spreadObjectOrFalsy.ts, 35, 21))
97+
}
98+
99+
class Foo<T extends string> {
100+
>Foo : Symbol(Foo, Decl(spreadObjectOrFalsy.ts, 37, 1))
101+
>T : Symbol(T, Decl(spreadObjectOrFalsy.ts, 39, 10))
102+
103+
data: T | undefined;
104+
>data : Symbol(Foo.data, Decl(spreadObjectOrFalsy.ts, 39, 29))
105+
>T : Symbol(T, Decl(spreadObjectOrFalsy.ts, 39, 10))
106+
107+
bar() {
108+
>bar : Symbol(Foo.bar, Decl(spreadObjectOrFalsy.ts, 40, 24))
109+
110+
if (this.hasData()) {
111+
>this.hasData : Symbol(Foo.hasData, Decl(spreadObjectOrFalsy.ts, 45, 5))
112+
>this : Symbol(Foo, Decl(spreadObjectOrFalsy.ts, 37, 1))
113+
>hasData : Symbol(Foo.hasData, Decl(spreadObjectOrFalsy.ts, 45, 5))
114+
115+
this.data.toLocaleLowerCase();
116+
>this.data.toLocaleLowerCase : Symbol(String.toLocaleLowerCase, Decl(lib.es5.d.ts, --, --))
117+
>this.data : Symbol(data, Decl(spreadObjectOrFalsy.ts, 39, 29), Decl(spreadObjectOrFalsy.ts, 35, 25))
118+
>data : Symbol(data, Decl(spreadObjectOrFalsy.ts, 39, 29), Decl(spreadObjectOrFalsy.ts, 35, 25))
119+
>toLocaleLowerCase : Symbol(String.toLocaleLowerCase, Decl(lib.es5.d.ts, --, --))
120+
}
121+
}
122+
hasData(): this is DatafulFoo<T> {
123+
>hasData : Symbol(Foo.hasData, Decl(spreadObjectOrFalsy.ts, 45, 5))
124+
>DatafulFoo : Symbol(DatafulFoo, Decl(spreadObjectOrFalsy.ts, 31, 1))
125+
>T : Symbol(T, Decl(spreadObjectOrFalsy.ts, 39, 10))
126+
127+
return true;
128+
}
129+
}
130+

tests/baselines/reference/spreadObjectOrFalsy.types

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,42 @@ function g1<T extends {}, A extends { z: (T | undefined) & T }>(a: A) {
7373
};
7474
}
7575

76+
// Repro from #47028
77+
78+
interface DatafulFoo<T> {
79+
data: T;
80+
>data : T
81+
}
82+
83+
class Foo<T extends string> {
84+
>Foo : Foo<T>
85+
86+
data: T | undefined;
87+
>data : T | undefined
88+
89+
bar() {
90+
>bar : () => void
91+
92+
if (this.hasData()) {
93+
>this.hasData() : boolean
94+
>this.hasData : () => this is DatafulFoo<T>
95+
>this : this
96+
>hasData : () => this is DatafulFoo<T>
97+
98+
this.data.toLocaleLowerCase();
99+
>this.data.toLocaleLowerCase() : string
100+
>this.data.toLocaleLowerCase : (locales?: string | string[] | undefined) => string
101+
>this.data : (T | undefined) & T
102+
>this : this & DatafulFoo<T>
103+
>data : (T | undefined) & T
104+
>toLocaleLowerCase : (locales?: string | string[] | undefined) => string
105+
}
106+
}
107+
hasData(): this is DatafulFoo<T> {
108+
>hasData : () => this is DatafulFoo<T>
109+
110+
return true;
111+
>true : true
112+
}
113+
}
114+

tests/cases/conformance/types/spread/spreadObjectOrFalsy.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,21 @@ function g1<T extends {}, A extends { z: (T | undefined) & T }>(a: A) {
3333
...z
3434
};
3535
}
36+
37+
// Repro from #47028
38+
39+
interface DatafulFoo<T> {
40+
data: T;
41+
}
42+
43+
class Foo<T extends string> {
44+
data: T | undefined;
45+
bar() {
46+
if (this.hasData()) {
47+
this.data.toLocaleLowerCase();
48+
}
49+
}
50+
hasData(): this is DatafulFoo<T> {
51+
return true;
52+
}
53+
}

0 commit comments

Comments
 (0)