Skip to content

Fix narrowing of intersection with function type #47483

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
Jan 21, 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
18 changes: 17 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ namespace ts {
EmptyObjectStrictFacts = All & ~(EQUndefined | EQNull | EQUndefinedOrNull),
AllTypeofNE = TypeofNEString | TypeofNENumber | TypeofNEBigInt | TypeofNEBoolean | TypeofNESymbol | TypeofNEObject | TypeofNEFunction | NEUndefined,
EmptyObjectFacts = All,
// Masks
OrFactsMask = TypeofEQFunction | TypeofEQObject | TypeofNEObject,
AndFactsMask = All & ~OrFactsMask,
}

const typeofEQFacts: ReadonlyESMap<string, TypeFacts> = new Map(getEntries({
Expand Down Expand Up @@ -22985,11 +22988,24 @@ namespace ts {
// When an intersection contains a primitive type we ignore object type constituents as they are
// presumably type tags. For example, in string & { __kind__: "name" } we ignore the object type.
ignoreObjects ||= maybeTypeOfKind(type, TypeFlags.Primitive);
return reduceLeft((type as UnionType).types, (facts, t) => facts & getTypeFacts(t, ignoreObjects), TypeFacts.All);
return getIntersectionTypeFacts(type as IntersectionType, ignoreObjects);
}
return TypeFacts.All;
}

function getIntersectionTypeFacts(type: IntersectionType, ignoreObjects: boolean): TypeFacts {
// When computing the type facts of an intersection type, certain type facts are computed as `and`
// and others are computed as `or`.
let oredFacts = TypeFacts.None;
let andedFacts = TypeFacts.All;
for (const t of type.types) {
const f = getTypeFacts(t, ignoreObjects);
oredFacts |= f;
andedFacts &= f;
}
return oredFacts & TypeFacts.OrFactsMask | andedFacts & TypeFacts.AndFactsMask;
}

function getTypeWithFacts(type: Type, include: TypeFacts) {
return filterType(type, t => (getTypeFacts(t) & include) !== 0);
}
Expand Down
40 changes: 40 additions & 0 deletions tests/baselines/reference/narrowingTypeofFunction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//// [narrowingTypeofFunction.ts]
type Meta = { foo: string }
interface F { (): string }

function f1(a: (F & Meta) | string) {
if (typeof a === "function") {
a;
}
else {
a;
}
}

function f2<T>(x: (T & F) | T & string) {
if (typeof x === "function") {
x;
}
else {
x;
}
}

//// [narrowingTypeofFunction.js]
"use strict";
function f1(a) {
if (typeof a === "function") {
a;
}
else {
a;
}
}
function f2(x) {
if (typeof x === "function") {
x;
}
else {
x;
}
}
45 changes: 45 additions & 0 deletions tests/baselines/reference/narrowingTypeofFunction.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
=== tests/cases/compiler/narrowingTypeofFunction.ts ===
type Meta = { foo: string }
>Meta : Symbol(Meta, Decl(narrowingTypeofFunction.ts, 0, 0))
>foo : Symbol(foo, Decl(narrowingTypeofFunction.ts, 0, 13))

interface F { (): string }
>F : Symbol(F, Decl(narrowingTypeofFunction.ts, 0, 27))

function f1(a: (F & Meta) | string) {
>f1 : Symbol(f1, Decl(narrowingTypeofFunction.ts, 1, 26))
>a : Symbol(a, Decl(narrowingTypeofFunction.ts, 3, 12))
>F : Symbol(F, Decl(narrowingTypeofFunction.ts, 0, 27))
>Meta : Symbol(Meta, Decl(narrowingTypeofFunction.ts, 0, 0))

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

a;
>a : Symbol(a, Decl(narrowingTypeofFunction.ts, 3, 12))
}
else {
a;
>a : Symbol(a, Decl(narrowingTypeofFunction.ts, 3, 12))
}
}

function f2<T>(x: (T & F) | T & string) {
>f2 : Symbol(f2, Decl(narrowingTypeofFunction.ts, 10, 1))
>T : Symbol(T, Decl(narrowingTypeofFunction.ts, 12, 12))
>x : Symbol(x, Decl(narrowingTypeofFunction.ts, 12, 15))
>T : Symbol(T, Decl(narrowingTypeofFunction.ts, 12, 12))
>F : Symbol(F, Decl(narrowingTypeofFunction.ts, 0, 27))
>T : Symbol(T, Decl(narrowingTypeofFunction.ts, 12, 12))

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

x;
>x : Symbol(x, Decl(narrowingTypeofFunction.ts, 12, 15))
}
else {
x;
>x : Symbol(x, Decl(narrowingTypeofFunction.ts, 12, 15))
}
}
44 changes: 44 additions & 0 deletions tests/baselines/reference/narrowingTypeofFunction.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
=== tests/cases/compiler/narrowingTypeofFunction.ts ===
type Meta = { foo: string }
>Meta : Meta
>foo : string

interface F { (): string }

function f1(a: (F & Meta) | string) {
>f1 : (a: (F & Meta) | string) => void
>a : string | (F & Meta)

if (typeof a === "function") {
>typeof a === "function" : boolean
>typeof a : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>a : string | (F & Meta)
>"function" : "function"

a;
>a : F & Meta
}
else {
a;
>a : string
}
}

function f2<T>(x: (T & F) | T & string) {
>f2 : <T>(x: (T & F) | (T & string)) => void
>x : (T & F) | (T & string)

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

x;
>x : (T & F) | (T & string)
}
else {
x;
>x : T & string
}
}
22 changes: 22 additions & 0 deletions tests/cases/compiler/narrowingTypeofFunction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// @strict: true

type Meta = { foo: string }
interface F { (): string }

function f1(a: (F & Meta) | string) {
if (typeof a === "function") {
a;
}
else {
a;
}
}

function f2<T>(x: (T & F) | T & string) {
if (typeof x === "function") {
x;
}
else {
x;
}
}