Skip to content

Don't special case nullable types in isTypeEqualityComparableTo #59559

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
11 changes: 6 additions & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39487,8 +39487,8 @@
}
}

function isTypeEqualityComparableTo(source: Type, target: Type) {
return (target.flags & TypeFlags.Nullable) !== 0 || isTypeComparableTo(source, target);
function isTypeEqualityComparableTo(source: Type, target: Type, strictEq: boolean) {
return !strictEq && (target.flags & TypeFlags.Nullable) !== 0 || isTypeComparableTo(source, target);
}

function createCheckBinaryExpression() {
Expand Down Expand Up @@ -39887,16 +39887,17 @@
// control flow analysis it is possible for operands to temporarily have narrower types, and those narrower
// types may cause the operands to not be comparable. We don't want such errors reported (see #46475).
if (!(checkMode && checkMode & CheckMode.TypeOnly)) {
const strictEq = operator === SyntaxKind.EqualsEqualsEqualsToken || operator === SyntaxKind.ExclamationEqualsEqualsToken;
if (
(isLiteralExpressionOfObject(left) || isLiteralExpressionOfObject(right)) &&
// only report for === and !== in JS, not == or !=
(!isInJSFile(left) || (operator === SyntaxKind.EqualsEqualsEqualsToken || operator === SyntaxKind.ExclamationEqualsEqualsToken))
(!isInJSFile(left) || strictEq)
) {
const eqType = operator === SyntaxKind.EqualsEqualsToken || operator === SyntaxKind.EqualsEqualsEqualsToken;
error(errorNode, Diagnostics.This_condition_will_always_return_0_since_JavaScript_compares_objects_by_reference_not_value, eqType ? "false" : "true");
}
checkNaNEquality(errorNode, operator, left, right);
reportOperatorErrorUnless((left, right) => isTypeEqualityComparableTo(left, right) || isTypeEqualityComparableTo(right, left));
reportOperatorErrorUnless((left, right) => isTypeEqualityComparableTo(left, right, strictEq) || isTypeEqualityComparableTo(right, left, strictEq));
}
return booleanType;
case SyntaxKind.InstanceOfKeyword:
Expand Down Expand Up @@ -45516,7 +45517,7 @@
// to or from the type of the 'switch' expression.
const caseType = checkExpression(clause.expression);

if (!isTypeEqualityComparableTo(expressionType, caseType)) {
if (!isTypeEqualityComparableTo(expressionType, caseType, /*strictEq*/ true)) {
// expressionType is not comparable to caseType, try the reversed check and report errors if it fails
checkTypeComparableTo(caseType, expressionType, clause.expression, /*headMessage*/ undefined);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
controlFlowCommaExpressionAssertionWithinTernary.ts(5,26): error TS2367: This comparison appears to be unintentional because the types 'number | null' and 'undefined' have no overlap.


==== controlFlowCommaExpressionAssertionWithinTernary.ts (1 errors) ====
declare function assert(value: any): asserts value;

function foo2(param: number | null | undefined): number | null {
const val = param !== undefined;
return val ? (assert(param !== undefined), param) : null;
~~~~~~~~~~~~~~~~~~~
!!! error TS2367: This comparison appears to be unintentional because the types 'number | null' and 'undefined' have no overlap.
// ^^^^^ Still typed as number | null | undefined
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ declare function assert(value: any): asserts value;
>assert : (value: any) => asserts value
> : ^ ^^ ^^^^^
>value : any
> : ^^^

function foo2(param: number | null | undefined): number | null {
>foo2 : (param: number | null | undefined) => number | null
Expand Down
5 changes: 4 additions & 1 deletion tests/baselines/reference/controlFlowIIFE.errors.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
controlFlowIIFE.ts(36,5): error TS2367: This comparison appears to be unintentional because the types 'number' and 'undefined' have no overlap.
controlFlowIIFE.ts(64,5): error TS2454: Variable 'v' is used before being assigned.
controlFlowIIFE.ts(72,5): error TS2454: Variable 'v' is used before being assigned.


==== controlFlowIIFE.ts (2 errors) ====
==== controlFlowIIFE.ts (3 errors) ====
declare function getStringOrNumber(): string | number;

function f1() {
Expand Down Expand Up @@ -39,6 +40,8 @@ controlFlowIIFE.ts(72,5): error TS2454: Variable 'v' is used before being assign
})();
maybeNumber++;
if (maybeNumber !== undefined) {
~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2367: This comparison appears to be unintentional because the types 'number' and 'undefined' have no overlap.
maybeNumber++;
}

Expand Down
11 changes: 10 additions & 1 deletion tests/baselines/reference/controlFlowOptionalChain.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ controlFlowOptionalChain.ts(223,9): error TS18048: 'o' is possibly 'undefined'.
controlFlowOptionalChain.ts(238,9): error TS18048: 'o' is possibly 'undefined'.
controlFlowOptionalChain.ts(241,9): error TS18048: 'o' is possibly 'undefined'.
controlFlowOptionalChain.ts(244,9): error TS18048: 'o' is possibly 'undefined'.
controlFlowOptionalChain.ts(270,9): error TS2367: This comparison appears to be unintentional because the types 'string | number | undefined' and 'null' have no overlap.
controlFlowOptionalChain.ts(271,9): error TS18048: 'o' is possibly 'undefined'.
controlFlowOptionalChain.ts(273,9): error TS2367: This comparison appears to be unintentional because the types 'string | number | undefined' and 'null' have no overlap.
controlFlowOptionalChain.ts(274,9): error TS18048: 'o' is possibly 'undefined'.
controlFlowOptionalChain.ts(276,9): error TS2367: This comparison appears to be unintentional because the types 'number | undefined' and 'null' have no overlap.
controlFlowOptionalChain.ts(277,9): error TS18048: 'o' is possibly 'undefined'.
controlFlowOptionalChain.ts(307,9): error TS18048: 'o' is possibly 'undefined'.
controlFlowOptionalChain.ts(310,9): error TS18048: 'o' is possibly 'undefined'.
Expand Down Expand Up @@ -61,7 +64,7 @@ controlFlowOptionalChain.ts(518,13): error TS18048: 'o' is possibly 'undefined'.
controlFlowOptionalChain.ts(567,21): error TS18048: 'someOptionalObject' is possibly 'undefined'.


==== controlFlowOptionalChain.ts (61 errors) ====
==== controlFlowOptionalChain.ts (64 errors) ====
// assignments in shortcutting chain
declare const o: undefined | {
[key: string]: any;
Expand Down Expand Up @@ -390,16 +393,22 @@ controlFlowOptionalChain.ts(567,21): error TS18048: 'someOptionalObject' is poss

function f13a(o: Thing | undefined) {
if (o?.foo !== null) {
~~~~~~~~~~~~~~~
!!! error TS2367: This comparison appears to be unintentional because the types 'string | number | undefined' and 'null' have no overlap.
o.foo; // Error
~
!!! error TS18048: 'o' is possibly 'undefined'.
}
if (o?.["foo"] !== null) {
~~~~~~~~~~~~~~~~~~~
!!! error TS2367: This comparison appears to be unintentional because the types 'string | number | undefined' and 'null' have no overlap.
o["foo"]; // Error
~
!!! error TS18048: 'o' is possibly 'undefined'.
}
if (o?.bar() !== null) {
~~~~~~~~~~~~~~~~~
!!! error TS2367: This comparison appears to be unintentional because the types 'number | undefined' and 'null' have no overlap.
o.bar; // Error
~
!!! error TS18048: 'o' is possibly 'undefined'.
Expand Down
114 changes: 114 additions & 0 deletions tests/baselines/reference/controlFlowOptionalChain2.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
controlFlowOptionalChain2.ts(71,9): error TS2367: This comparison appears to be unintentional because the types 'string | undefined' and 'null' have no overlap.
controlFlowOptionalChain2.ts(89,9): error TS2367: This comparison appears to be unintentional because the types 'string | undefined' and 'null' have no overlap.


==== controlFlowOptionalChain2.ts (2 errors) ====
type A = {
type: 'A';
name: string;
}

type B = {
type: 'B';
}

function funcTwo(arg: A | B | undefined) {
if (arg?.type === 'B') {
arg; // `B`
return;
}

arg;
arg?.name;
}

function funcThree(arg: A | B | null) {
if (arg?.type === 'B') {
arg; // `B`
return;
}

arg;
arg?.name;
}

type U = { kind: undefined, u: 'u' }
type N = { kind: null, n: 'n' }
type X = { kind: 'X', x: 'x' }

function f1(x: X | U | undefined) {
if (x?.kind === undefined) {
x; // U | undefined
}
else {
x; // X
}
}

function f2(x: X | N | undefined) {
if (x?.kind === undefined) {
x; // undefined
}
else {
x; // X | N
}
}

function f3(x: X | U | null) {
if (x?.kind === undefined) {
x; // U | null
}
else {
x; // X
}
}

function f4(x: X | N | null) {
if (x?.kind === undefined) {
x; // null
}
else {
x; // X | N
}
}

function f5(x: X | U | undefined) {
if (x?.kind === null) {
~~~~~~~~~~~~~~~~
!!! error TS2367: This comparison appears to be unintentional because the types 'string | undefined' and 'null' have no overlap.
x; // never
}
else {
x; // X | U | undefined
}
}

function f6(x: X | N | undefined) {
if (x?.kind === null) {
x; // N
}
else {
x; // X | undefined
}
}

function f7(x: X | U | null) {
if (x?.kind === null) {
~~~~~~~~~~~~~~~~
!!! error TS2367: This comparison appears to be unintentional because the types 'string | undefined' and 'null' have no overlap.
x; // never
}
else {
x; // X | U | null
}
}

function f8(x: X | N | null) {
if (x?.kind === null) {
x; // N
}
else {
x; // X | null
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
emptyAnonymousObjectNarrowing.ts(43,5): error TS2367: This comparison appears to be unintentional because the types '{}' and 'undefined' have no overlap.
emptyAnonymousObjectNarrowing.ts(50,5): error TS2367: This comparison appears to be unintentional because the types '{}' and 'null' have no overlap.


==== emptyAnonymousObjectNarrowing.ts (2 errors) ====
declare let nonNull: {};
if (nonNull === "foo") {
nonNull;
}
else {
nonNull;
}

declare let obj: { a: string };
if (nonNull === obj) {
nonNull;
}
else {
nonNull;
}

function f1<T>(x: T) {
if (nonNull === x) {
nonNull;
}
else {
nonNull;
}
}

function f2<T extends object>(x: T) {
if (nonNull === x) {
nonNull;
}
else {
nonNull;
}
}

declare let union: "xyz" | { a: string } | undefined;
if (nonNull === union) {
nonNull;
}
else {
nonNull;
}

if (nonNull === undefined) {
~~~~~~~~~~~~~~~~~~~~~
!!! error TS2367: This comparison appears to be unintentional because the types '{}' and 'undefined' have no overlap.
nonNull;
}
else {
nonNull;
}

if (nonNull === null) {
~~~~~~~~~~~~~~~~
!!! error TS2367: This comparison appears to be unintentional because the types '{}' and 'null' have no overlap.
nonNull;
}
else {
nonNull;
}

if (nonNull == undefined) {
nonNull;
}
else {
nonNull;
}

// Repro from #50567
const foo = (value: unknown): string => {
if (!value) {
return 'foo';
}
if (value === 'xyz') {
return value; // Type '{}' is not assignable to type 'string'.
}
return '';
};

Loading
Loading