Skip to content

Revert change to getFalsyFlags #47125

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

Merged
merged 2 commits into from
Dec 13, 2021
Merged
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
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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;
}
}