Skip to content

Commit

Permalink
Revert change to getFalsyFlags (#47125)
Browse files Browse the repository at this point in the history
* Revert change to getFalsyFlags

* Add regression test
  • Loading branch information
ahejlsberg committed Dec 13, 2021
1 parent d417058 commit 06746ef
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Expand Up @@ -20893,7 +20893,7 @@ namespace ts {
// flags for the string, number, boolean, "", 0, false, void, undefined, or null types respectively. Returns
// no flags for all other types (including non-falsy literal types).
function getFalsyFlags(type: Type): TypeFlags {
return type.flags & TypeFlags.UnionOrIntersection ? getFalsyFlagsOfTypes((type as UnionType).types) :
return type.flags & TypeFlags.Union ? getFalsyFlagsOfTypes((type as UnionType).types) :
type.flags & TypeFlags.StringLiteral ? (type as StringLiteralType).value === "" ? TypeFlags.StringLiteral : 0 :
type.flags & TypeFlags.NumberLiteral ? (type as NumberLiteralType).value === 0 ? TypeFlags.NumberLiteral : 0 :
type.flags & TypeFlags.BigIntLiteral ? isZeroBigInt(type as BigIntLiteralType) ? TypeFlags.BigIntLiteral : 0 :
Expand Down
18 changes: 18 additions & 0 deletions tests/baselines/reference/spreadObjectOrFalsy.errors.txt
Expand Up @@ -39,4 +39,22 @@ tests/cases/conformance/types/spread/spreadObjectOrFalsy.ts(10,14): error TS2698
...z
};
}

// Repro from #47028

interface DatafulFoo<T> {
data: T;
}

class Foo<T extends string> {
data: T | undefined;
bar() {
if (this.hasData()) {
this.data.toLocaleLowerCase();
}
}
hasData(): this is DatafulFoo<T> {
return true;
}
}

39 changes: 39 additions & 0 deletions tests/baselines/reference/spreadObjectOrFalsy.js
Expand Up @@ -31,6 +31,24 @@ function g1<T extends {}, A extends { z: (T | undefined) & T }>(a: A) {
...z
};
}

// Repro from #47028

interface DatafulFoo<T> {
data: T;
}

class Foo<T extends string> {
data: T | undefined;
bar() {
if (this.hasData()) {
this.data.toLocaleLowerCase();
}
}
hasData(): this is DatafulFoo<T> {
return true;
}
}


//// [spreadObjectOrFalsy.js]
Expand Down Expand Up @@ -69,6 +87,19 @@ function g1(a) {
var z = a.z;
return __assign({}, z);
}
var Foo = /** @class */ (function () {
function Foo() {
}
Foo.prototype.bar = function () {
if (this.hasData()) {
this.data.toLocaleLowerCase();
}
};
Foo.prototype.hasData = function () {
return true;
};
return Foo;
}());


//// [spreadObjectOrFalsy.d.ts]
Expand All @@ -81,3 +112,11 @@ declare function f6<T extends object | undefined>(a: T): T;
declare function g1<T extends {}, A extends {
z: (T | undefined) & T;
}>(a: A): (T | undefined) & T;
interface DatafulFoo<T> {
data: T;
}
declare class Foo<T extends string> {
data: T | undefined;
bar(): void;
hasData(): this is DatafulFoo<T>;
}
43 changes: 43 additions & 0 deletions tests/baselines/reference/spreadObjectOrFalsy.symbols
Expand Up @@ -85,3 +85,46 @@ function g1<T extends {}, A extends { z: (T | undefined) & T }>(a: A) {
};
}

// Repro from #47028

interface DatafulFoo<T> {
>DatafulFoo : Symbol(DatafulFoo, Decl(spreadObjectOrFalsy.ts, 31, 1))
>T : Symbol(T, Decl(spreadObjectOrFalsy.ts, 35, 21))

data: T;
>data : Symbol(DatafulFoo.data, Decl(spreadObjectOrFalsy.ts, 35, 25))
>T : Symbol(T, Decl(spreadObjectOrFalsy.ts, 35, 21))
}

class Foo<T extends string> {
>Foo : Symbol(Foo, Decl(spreadObjectOrFalsy.ts, 37, 1))
>T : Symbol(T, Decl(spreadObjectOrFalsy.ts, 39, 10))

data: T | undefined;
>data : Symbol(Foo.data, Decl(spreadObjectOrFalsy.ts, 39, 29))
>T : Symbol(T, Decl(spreadObjectOrFalsy.ts, 39, 10))

bar() {
>bar : Symbol(Foo.bar, Decl(spreadObjectOrFalsy.ts, 40, 24))

if (this.hasData()) {
>this.hasData : Symbol(Foo.hasData, Decl(spreadObjectOrFalsy.ts, 45, 5))
>this : Symbol(Foo, Decl(spreadObjectOrFalsy.ts, 37, 1))
>hasData : Symbol(Foo.hasData, Decl(spreadObjectOrFalsy.ts, 45, 5))

this.data.toLocaleLowerCase();
>this.data.toLocaleLowerCase : Symbol(String.toLocaleLowerCase, Decl(lib.es5.d.ts, --, --))
>this.data : Symbol(data, Decl(spreadObjectOrFalsy.ts, 39, 29), Decl(spreadObjectOrFalsy.ts, 35, 25))
>data : Symbol(data, Decl(spreadObjectOrFalsy.ts, 39, 29), Decl(spreadObjectOrFalsy.ts, 35, 25))
>toLocaleLowerCase : Symbol(String.toLocaleLowerCase, Decl(lib.es5.d.ts, --, --))
}
}
hasData(): this is DatafulFoo<T> {
>hasData : Symbol(Foo.hasData, Decl(spreadObjectOrFalsy.ts, 45, 5))
>DatafulFoo : Symbol(DatafulFoo, Decl(spreadObjectOrFalsy.ts, 31, 1))
>T : Symbol(T, Decl(spreadObjectOrFalsy.ts, 39, 10))

return true;
}
}

39 changes: 39 additions & 0 deletions tests/baselines/reference/spreadObjectOrFalsy.types
Expand Up @@ -73,3 +73,42 @@ function g1<T extends {}, A extends { z: (T | undefined) & T }>(a: A) {
};
}

// Repro from #47028

interface DatafulFoo<T> {
data: T;
>data : T
}

class Foo<T extends string> {
>Foo : Foo<T>

data: T | undefined;
>data : T | undefined

bar() {
>bar : () => void

if (this.hasData()) {
>this.hasData() : boolean
>this.hasData : () => this is DatafulFoo<T>
>this : this
>hasData : () => this is DatafulFoo<T>

this.data.toLocaleLowerCase();
>this.data.toLocaleLowerCase() : string
>this.data.toLocaleLowerCase : (locales?: string | string[] | undefined) => string
>this.data : (T | undefined) & T
>this : this & DatafulFoo<T>
>data : (T | undefined) & T
>toLocaleLowerCase : (locales?: string | string[] | undefined) => string
}
}
hasData(): this is DatafulFoo<T> {
>hasData : () => this is DatafulFoo<T>

return true;
>true : true
}
}

18 changes: 18 additions & 0 deletions tests/cases/conformance/types/spread/spreadObjectOrFalsy.ts
Expand Up @@ -33,3 +33,21 @@ function g1<T extends {}, A extends { z: (T | undefined) & T }>(a: A) {
...z
};
}

// Repro from #47028

interface DatafulFoo<T> {
data: T;
}

class Foo<T extends string> {
data: T | undefined;
bar() {
if (this.hasData()) {
this.data.toLocaleLowerCase();
}
}
hasData(): this is DatafulFoo<T> {
return true;
}
}

0 comments on commit 06746ef

Please sign in to comment.