Skip to content

Fix narrowing of optional chains #36089

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 3 commits into from
Jan 8, 2020
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
23 changes: 16 additions & 7 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19777,13 +19777,22 @@ namespace ts {
}

function narrowTypeByOptionalChainContainment(type: Type, operator: SyntaxKind, value: Expression, assumeTrue: boolean): Type {
// We are in a branch of obj?.foo === value or obj?.foo !== value. We remove undefined and null from
// the type of obj if (a) the operator is === and the type of value doesn't include undefined or (b) the
// operator is !== and the type of value is undefined.
const effectiveTrue = operator === SyntaxKind.EqualsEqualsToken || operator === SyntaxKind.EqualsEqualsEqualsToken ? assumeTrue : !assumeTrue;
const doubleEquals = operator === SyntaxKind.EqualsEqualsToken || operator === SyntaxKind.ExclamationEqualsToken;
const valueNonNullish = !(getTypeFacts(getTypeOfExpression(value)) & (doubleEquals ? TypeFacts.EQUndefinedOrNull : TypeFacts.EQUndefined));
return effectiveTrue === valueNonNullish ? getTypeWithFacts(type, TypeFacts.NEUndefinedOrNull) : type;
// We are in a branch of obj?.foo === value (or any one of the other equality operators). We narrow obj as follows:
// When operator is === and type of value excludes undefined, null and undefined is removed from type of obj in true branch.
// When operator is !== and type of value excludes undefined, null and undefined is removed from type of obj in false branch.
// When operator is == and type of value excludes null and undefined, null and undefined is removed from type of obj in true branch.
// When operator is != and type of value excludes null and undefined, null and undefined is removed from type of obj in false branch.
// When operator is === and type of value is undefined, null and undefined is removed from type of obj in false branch.
// When operator is !== and type of value is undefined, null and undefined is removed from type of obj in true branch.
// When operator is == and type of value is null or undefined, null and undefined is removed from type of obj in false branch.
// When operator is != and type of value is null or undefined, null and undefined is removed from type of obj in true branch.
Copy link
Member

Choose a reason for hiding this comment

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

🤓

const equalsOperator = operator === SyntaxKind.EqualsEqualsToken || operator === SyntaxKind.EqualsEqualsEqualsToken;
const nullableFlags = operator === SyntaxKind.EqualsEqualsToken || operator === SyntaxKind.ExclamationEqualsToken ? TypeFlags.Nullable : TypeFlags.Undefined;
const valueType = getTypeOfExpression(value);
// Note that we include any and unknown in the exclusion test because their domain includes null and undefined.
const removeNullable = equalsOperator !== assumeTrue && everyType(valueType, t => !!(t.flags & nullableFlags)) ||
equalsOperator === assumeTrue && everyType(valueType, t => !(t.flags & (TypeFlags.AnyOrUnknown | nullableFlags)));
return removeNullable ? getTypeWithFacts(type, TypeFacts.NEUndefinedOrNull) : type;
}

function narrowTypeByEquality(type: Type, operator: SyntaxKind, value: Expression, assumeTrue: boolean): Type {
Expand Down
103 changes: 90 additions & 13 deletions tests/baselines/reference/controlFlowOptionalChain.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,33 @@ tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(310,9): error TS
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(319,9): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(322,9): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(331,9): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(334,9): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(337,9): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(340,9): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(343,9): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(346,9): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(349,9): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(352,9): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(391,9): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(394,9): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(403,9): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(406,9): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(415,9): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(424,9): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(427,9): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(436,9): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(471,13): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(474,13): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(488,13): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(491,13): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(358,9): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(367,9): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(370,9): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(379,9): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(418,9): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(421,9): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(430,9): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(433,9): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(442,9): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(451,9): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(454,9): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(463,9): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(498,13): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(501,13): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(515,13): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(518,13): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(567,21): error TS2532: Object is possibly 'undefined'.


==== tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts (53 errors) ====
==== tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts (62 errors) ====
// assignments in shortcutting chain
declare const o: undefined | {
[key: string]: any;
Expand Down Expand Up @@ -456,6 +465,49 @@ tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(491,13): error T
}
}

function f15a(o: Thing | undefined, value: unknown) {
if (o?.foo === value) {
o.foo; // Error
~
!!! error TS2532: Object is possibly 'undefined'.
}
else {
o.foo; // Error
~
!!! error TS2532: Object is possibly 'undefined'.
}
if (o?.foo !== value) {
o.foo; // Error
~
!!! error TS2532: Object is possibly 'undefined'.
}
else {
o.foo; // Error
~
!!! error TS2532: Object is possibly 'undefined'.
}
if (o?.foo == value) {
o.foo; // Error
~
!!! error TS2532: Object is possibly 'undefined'.
}
else {
o.foo; // Error
~
!!! error TS2532: Object is possibly 'undefined'.
}
if (o?.foo != value) {
o.foo; // Error
~
!!! error TS2532: Object is possibly 'undefined'.
}
else {
o.foo; // Error
~
!!! error TS2532: Object is possibly 'undefined'.
}
}

function f16(o: Thing | undefined) {
if (o?.foo === undefined) {
o.foo; // Error
Expand Down Expand Up @@ -687,4 +739,29 @@ tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(491,13): error T
}
return f.geometry.coordinates;
}

// Repro from #35842

interface SomeObject {
someProperty: unknown;
}

let lastSomeProperty: unknown | undefined;

function someFunction(someOptionalObject: SomeObject | undefined): void {
if (someOptionalObject?.someProperty !== lastSomeProperty) {
console.log(someOptionalObject);
console.log(someOptionalObject.someProperty); // Error
~~~~~~~~~~~~~~~~~~
!!! error TS2532: Object is possibly 'undefined'.
lastSomeProperty = someOptionalObject?.someProperty;
}
}

const someObject: SomeObject = {
someProperty: 42
};

someFunction(someObject);
someFunction(undefined);

89 changes: 89 additions & 0 deletions tests/baselines/reference/controlFlowOptionalChain.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,33 @@ function f15(o: Thing | undefined, value: number) {
}
}

function f15a(o: Thing | undefined, value: unknown) {
if (o?.foo === value) {
o.foo; // Error
}
else {
o.foo; // Error
}
if (o?.foo !== value) {
o.foo; // Error
}
else {
o.foo; // Error
}
if (o?.foo == value) {
o.foo; // Error
}
else {
o.foo; // Error
}
if (o?.foo != value) {
o.foo; // Error
}
else {
o.foo; // Error
}
}

function f16(o: Thing | undefined) {
if (o?.foo === undefined) {
o.foo; // Error
Expand Down Expand Up @@ -526,6 +553,29 @@ function extractCoordinates(f: Feature): number[] {
}
return f.geometry.coordinates;
}

// Repro from #35842

interface SomeObject {
someProperty: unknown;
}

let lastSomeProperty: unknown | undefined;

function someFunction(someOptionalObject: SomeObject | undefined): void {
if (someOptionalObject?.someProperty !== lastSomeProperty) {
console.log(someOptionalObject);
console.log(someOptionalObject.someProperty); // Error
lastSomeProperty = someOptionalObject?.someProperty;
}
}

const someObject: SomeObject = {
someProperty: 42
};

someFunction(someObject);
someFunction(undefined);


//// [controlFlowOptionalChain.js]
Expand Down Expand Up @@ -809,6 +859,32 @@ function f15(o, value) {
o.foo;
}
}
function f15a(o, value) {
if ((o === null || o === void 0 ? void 0 : o.foo) === value) {
o.foo; // Error
}
else {
o.foo; // Error
}
if ((o === null || o === void 0 ? void 0 : o.foo) !== value) {
o.foo; // Error
}
else {
o.foo; // Error
}
if ((o === null || o === void 0 ? void 0 : o.foo) == value) {
o.foo; // Error
}
else {
o.foo; // Error
}
if ((o === null || o === void 0 ? void 0 : o.foo) != value) {
o.foo; // Error
}
else {
o.foo; // Error
}
}
function f16(o) {
if ((o === null || o === void 0 ? void 0 : o.foo) === undefined) {
o.foo; // Error
Expand Down Expand Up @@ -982,3 +1058,16 @@ function extractCoordinates(f) {
}
return f.geometry.coordinates;
}
var lastSomeProperty;
function someFunction(someOptionalObject) {
if ((someOptionalObject === null || someOptionalObject === void 0 ? void 0 : someOptionalObject.someProperty) !== lastSomeProperty) {
console.log(someOptionalObject);
console.log(someOptionalObject.someProperty); // Error
lastSomeProperty = someOptionalObject === null || someOptionalObject === void 0 ? void 0 : someOptionalObject.someProperty;
}
}
var someObject = {
someProperty: 42
};
someFunction(someObject);
someFunction(undefined);
Loading