Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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
14 changes: 11 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ namespace ts {
TypeofNEString = 1 << 8, // typeof x !== "string"
TypeofNENumber = 1 << 9, // typeof x !== "number"
TypeofNEBigInt = 1 << 10, // typeof x !== "bigint"
TypeofNEBoolean = 1 << 11, // typeof x !== "boolean"
TypeofNEBoolean = 1 << 11, // typeof x !== "boolean"
TypeofNESymbol = 1 << 12, // typeof x !== "symbol"
TypeofNEObject = 1 << 13, // typeof x !== "object"
TypeofNEFunction = 1 << 14, // typeof x !== "function"
Expand All @@ -90,6 +90,7 @@ namespace ts {
// The following members encode facts about particular kinds of types for use in the getTypeFacts function.
// The presence of a particular fact means that the given test is true for some (and possibly all) values
// of that kind of type.
NegativeTypeofFacts = TypeofNEString | TypeofNENumber | TypeofNEBigInt | TypeofNEBoolean | TypeofNESymbol | TypeofNEObject | TypeofNEFunction | TypeofNEHostObject,
BaseStringStrictFacts = TypeofEQString | TypeofNENumber | TypeofNEBigInt | TypeofNEBoolean | TypeofNESymbol | TypeofNEObject | TypeofNEFunction | TypeofNEHostObject | NEUndefined | NENull | NEUndefinedOrNull,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NegativeTypeofFacts is only ever used to left-shift 8 bits. It would be easier to read if it were just PositiveTypeofFacts, which would need shifting

BaseStringFacts = BaseStringStrictFacts | EQUndefined | EQNull | EQUndefinedOrNull | Falsy,
StringStrictFacts = BaseStringStrictFacts | Truthy | Falsy,
Expand Down Expand Up @@ -19701,11 +19702,18 @@ namespace ts {
return declaredType;
}

function getTypeFactsOfTypes(types: Type[]): TypeFacts {
function getTypeFactsOfTypes(types: Type[], isUnion: boolean): TypeFacts {
let result: TypeFacts = TypeFacts.None;
for (const t of types) {
result |= getTypeFacts(t);
}
if (!isUnion) {
// Get the set of positive facts for the intersection by masking with the negative set shifted left,
// then shift those present positive facts into the negative fact value range, and unset any of those
// bits (by negating that mask and then intersecting it with the original value)
const positiveFacts = result & (TypeFacts.NegativeTypeofFacts >> 8);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like the test coverage here is not adequate to ensure that this math continues to work for all cases in the future. Let's have a testcase that makes sure that e.g. someone reordering the EQ members without also pairwise reordering the NE members sees a baseline diff.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not quite sure how to make a baseline for that, but a unittest can be made if the TypeFacts gets export + @internal:

describe("getTypeFactsOfTypes", () => {
    // See: https://github.com/microsoft/TypeScript/pull/39016
    it("correctly can strip NegativeTypeofFacts when there are PositiveTypeofFacts of the same type via bitmask logic", () => {
        const TypeFacts = (ts as any).TypeFacts;

        // For a set of facts, which include both the positive and negative of each other
        let ExampleFacts = TypeFacts.TypeofEQNumber | TypeFacts.TypeofNENumber | TypeFacts.TypeofNEBigInt;
        // Using this line of code (with the 8 which effectively represents the number of EQ types)
        const positiveFacts = ExampleFacts & (TypeFacts.NegativeTypeofFacts >> 8);
        // Then this should drop the negative version of the same type
        ExampleFacts &= ~(positiveFacts << 8);
        assert.isTrue((ExampleFacts & TypeFacts.TypeofNENumber) === 0);

        // And it keeps the rest
        assert.isTrue((ExampleFacts & TypeFacts.TypeofEQNumber) !== 0);
        assert.isTrue((ExampleFacts & TypeFacts.TypeofNEBigInt) !== 0);

        // If this test has failed, you _probably_ need to adjust the 8 here, and in
        // getTypeFactsOfTypes.
    });
});```

result &= ~(positiveFacts << 8);
}
return result;
}

Expand Down Expand Up @@ -19780,7 +19788,7 @@ namespace ts {
return getTypeFacts(getBaseConstraintOfType(type) || unknownType);
}
if (flags & TypeFlags.UnionOrIntersection) {
return getTypeFactsOfTypes((<UnionOrIntersectionType>type).types);
return getTypeFactsOfTypes((<UnionOrIntersectionType>type).types, !!(flags & TypeFlags.Union));
}
return TypeFacts.All;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//// [controlFlowTypeofFunctionElseNarrowing.ts]
// regression for https://github.com/microsoft/TypeScript/issues/32928
// Callable
type F = (...args: any[]) => any;
// Callable but intersected
type F2 = F & { inject?: string[] }

declare const a: string | F2

if (typeof a == 'function') {
// only F2
a
} else {
// Should be only a string
a
}

//// [controlFlowTypeofFunctionElseNarrowing.js]
if (typeof a == 'function') {
// only F2
a;
}
else {
// Should be only a string
a;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
=== tests/cases/conformance/controlFlow/controlFlowTypeofFunctionElseNarrowing.ts ===
// regression for https://github.com/microsoft/TypeScript/issues/32928
// Callable
type F = (...args: any[]) => any;
>F : Symbol(F, Decl(controlFlowTypeofFunctionElseNarrowing.ts, 0, 0))
>args : Symbol(args, Decl(controlFlowTypeofFunctionElseNarrowing.ts, 2, 10))

// Callable but intersected
type F2 = F & { inject?: string[] }
>F2 : Symbol(F2, Decl(controlFlowTypeofFunctionElseNarrowing.ts, 2, 33))
>F : Symbol(F, Decl(controlFlowTypeofFunctionElseNarrowing.ts, 0, 0))
>inject : Symbol(inject, Decl(controlFlowTypeofFunctionElseNarrowing.ts, 4, 15))

declare const a: string | F2
>a : Symbol(a, Decl(controlFlowTypeofFunctionElseNarrowing.ts, 6, 13))
>F2 : Symbol(F2, Decl(controlFlowTypeofFunctionElseNarrowing.ts, 2, 33))

if (typeof a == 'function') {
>a : Symbol(a, Decl(controlFlowTypeofFunctionElseNarrowing.ts, 6, 13))

// only F2
a
>a : Symbol(a, Decl(controlFlowTypeofFunctionElseNarrowing.ts, 6, 13))

} else {
// Should be only a string
a
>a : Symbol(a, Decl(controlFlowTypeofFunctionElseNarrowing.ts, 6, 13))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
=== tests/cases/conformance/controlFlow/controlFlowTypeofFunctionElseNarrowing.ts ===
// regression for https://github.com/microsoft/TypeScript/issues/32928
// Callable
type F = (...args: any[]) => any;
>F : F
>args : any[]

// Callable but intersected
type F2 = F & { inject?: string[] }
>F2 : F2
>inject : string[]

declare const a: string | F2
>a : string | F2

if (typeof a == 'function') {
>typeof a == 'function' : boolean
>typeof a : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>a : string | F2
>'function' : "function"

// only F2
a
>a : F2

} else {
// Should be only a string
a
>a : string
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// regression for https://github.com/microsoft/TypeScript/issues/32928
// Callable
type F = (...args: any[]) => any;
// Callable but intersected
type F2 = F & { inject?: string[] }

declare const a: string | F2

if (typeof a == 'function') {
// only F2
a
} else {
// Should be only a string
a
}