Skip to content

fix typefacts of intersection #47583

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 1 commit into from
Feb 1, 2022
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
7 changes: 5 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ namespace ts {
AllTypeofNE = TypeofNEString | TypeofNENumber | TypeofNEBigInt | TypeofNEBoolean | TypeofNESymbol | TypeofNEObject | TypeofNEFunction | NEUndefined,
EmptyObjectFacts = All,
// Masks
OrFactsMask = TypeofEQFunction | TypeofEQObject | TypeofNEObject,
OrFactsMask = TypeofEQFunction | TypeofNEObject,
AndFactsMask = All & ~OrFactsMask,
}

Expand Down Expand Up @@ -22962,7 +22962,10 @@ namespace ts {
(type === falseType || type === regularFalseType) ? TypeFacts.FalseStrictFacts : TypeFacts.TrueStrictFacts :
(type === falseType || type === regularFalseType) ? TypeFacts.FalseFacts : TypeFacts.TrueFacts;
}
if (flags & TypeFlags.Object && !ignoreObjects) {
if (flags & TypeFlags.Object) {
if (ignoreObjects) {
return TypeFacts.AndFactsMask; // This is the identity element for computing type facts of intersection.
}
return getObjectFlags(type) & ObjectFlags.Anonymous && isEmptyObjectType(type as ObjectType) ?
strictNullChecks ? TypeFacts.EmptyObjectStrictFacts : TypeFacts.EmptyObjectFacts :
isFunctionObjectType(type as ObjectType) ?
Expand Down
11 changes: 11 additions & 0 deletions tests/baselines/reference/narrowingTypeofFunction.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ function f2<T>(x: (T & F) | T & string) {
else {
x;
}
}

function f3(x: { _foo: number } & number) {
if (typeof x === "function") {
x;
}
}

//// [narrowingTypeofFunction.js]
Expand All @@ -38,3 +44,8 @@ function f2(x) {
x;
}
}
function f3(x) {
if (typeof x === "function") {
x;
}
}
13 changes: 13 additions & 0 deletions tests/baselines/reference/narrowingTypeofFunction.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,16 @@ function f2<T>(x: (T & F) | T & string) {
>x : Symbol(x, Decl(narrowingTypeofFunction.ts, 12, 15))
}
}

function f3(x: { _foo: number } & number) {
>f3 : Symbol(f3, Decl(narrowingTypeofFunction.ts, 19, 1))
>x : Symbol(x, Decl(narrowingTypeofFunction.ts, 21, 12))
>_foo : Symbol(_foo, Decl(narrowingTypeofFunction.ts, 21, 16))

if (typeof x === "function") {
>x : Symbol(x, Decl(narrowingTypeofFunction.ts, 21, 12))

x;
>x : Symbol(x, Decl(narrowingTypeofFunction.ts, 21, 12))
}
}
16 changes: 16 additions & 0 deletions tests/baselines/reference/narrowingTypeofFunction.types
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,19 @@ function f2<T>(x: (T & F) | T & string) {
>x : T & string
}
}

function f3(x: { _foo: number } & number) {
>f3 : (x: { _foo: number;} & number) => void
>x : { _foo: number; } & number
>_foo : number

if (typeof x === "function") {
>typeof x === "function" : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : { _foo: number; } & number
>"function" : "function"

x;
>x : never
}
}
27 changes: 27 additions & 0 deletions tests/baselines/reference/narrowingTypeofObject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//// [narrowingTypeofObject.ts]
interface F { (): string }

function test(x: number & { _foo: string }) {
if (typeof x === 'object') {
x;
}
}

function f1(x: F & { foo: number }) {
if (typeof x !== "object") {
x;
}
}

//// [narrowingTypeofObject.js]
"use strict";
function test(x) {
if (typeof x === 'object') {
x;
}
}
function f1(x) {
if (typeof x !== "object") {
x;
}
}
30 changes: 30 additions & 0 deletions tests/baselines/reference/narrowingTypeofObject.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
=== tests/cases/compiler/narrowingTypeofObject.ts ===
interface F { (): string }
>F : Symbol(F, Decl(narrowingTypeofObject.ts, 0, 0))

function test(x: number & { _foo: string }) {
>test : Symbol(test, Decl(narrowingTypeofObject.ts, 0, 26))
>x : Symbol(x, Decl(narrowingTypeofObject.ts, 2, 14))
>_foo : Symbol(_foo, Decl(narrowingTypeofObject.ts, 2, 27))

if (typeof x === 'object') {
>x : Symbol(x, Decl(narrowingTypeofObject.ts, 2, 14))

x;
>x : Symbol(x, Decl(narrowingTypeofObject.ts, 2, 14))
}
}

function f1(x: F & { foo: number }) {
>f1 : Symbol(f1, Decl(narrowingTypeofObject.ts, 6, 1))
>x : Symbol(x, Decl(narrowingTypeofObject.ts, 8, 12))
>F : Symbol(F, Decl(narrowingTypeofObject.ts, 0, 0))
>foo : Symbol(foo, Decl(narrowingTypeofObject.ts, 8, 20))

if (typeof x !== "object") {
>x : Symbol(x, Decl(narrowingTypeofObject.ts, 8, 12))

x;
>x : Symbol(x, Decl(narrowingTypeofObject.ts, 8, 12))
}
}
34 changes: 34 additions & 0 deletions tests/baselines/reference/narrowingTypeofObject.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
=== tests/cases/compiler/narrowingTypeofObject.ts ===
interface F { (): string }

function test(x: number & { _foo: string }) {
>test : (x: number & { _foo: string;}) => void
>x : number & { _foo: string; }
>_foo : string

if (typeof x === 'object') {
>typeof x === 'object' : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : number & { _foo: string; }
>'object' : "object"

x;
>x : never
}
}

function f1(x: F & { foo: number }) {
>f1 : (x: F & { foo: number;}) => void
>x : F & { foo: number; }
>foo : number

if (typeof x !== "object") {
>typeof x !== "object" : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : F & { foo: number; }
>"object" : "object"

x;
>x : F & { foo: number; }
}
}
6 changes: 6 additions & 0 deletions tests/cases/compiler/narrowingTypeofFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@ function f2<T>(x: (T & F) | T & string) {
else {
x;
}
}

function f3(x: { _foo: number } & number) {
if (typeof x === "function") {
x;
}
}
15 changes: 15 additions & 0 deletions tests/cases/compiler/narrowingTypeofObject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// @strict: true

interface F { (): string }

function test(x: number & { _foo: string }) {
if (typeof x === 'object') {
x;
}
}

function f1(x: F & { foo: number }) {
if (typeof x !== "object") {
x;
}
}