Skip to content
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

Fix(15506) - Disallow non-any/number/string operands on comparisons #56666

Open
wants to merge 13 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
29 changes: 27 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38382,13 +38382,38 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
leftType = getBaseTypeOfLiteralTypeForComparison(checkNonNullType(leftType, left));
rightType = getBaseTypeOfLiteralTypeForComparison(checkNonNullType(rightType, right));
reportOperatorErrorUnless((left, right) => {
const isLeftTypeComparable = !!(
isTypeAssignableTo(left, numberOrBigIntType) ||
isTypeAssignableTo(left, stringType) ||
(isTypeAssignableTo(left, anyType) &&
!isTypeAssignableTo(left, voidType) &&
!isTypeAssignableTo(left, booleanType) &&
!isTypeAssignableTo(left, emptyGenericType) &&
!(left.flags & TypeFlags.Object))
);

const isRightTypeComparable = !!(
isTypeAssignableTo(right, numberOrBigIntType) ||
isTypeAssignableTo(right, stringType) ||
(isTypeAssignableTo(right, anyType) &&
!isTypeAssignableTo(right, voidType) &&
!isTypeAssignableTo(right, booleanType) &&
!isTypeAssignableTo(right, emptyGenericType) &&
!(right.flags & TypeFlags.Object))
);

if (!isLeftTypeComparable || !isRightTypeComparable) {
return false;
}

if (isTypeAny(left) || isTypeAny(right)) {
return true;
}

const leftAssignableToNumber = isTypeAssignableTo(left, numberOrBigIntType);
const rightAssignableToNumber = isTypeAssignableTo(right, numberOrBigIntType);
return leftAssignableToNumber && rightAssignableToNumber ||
!leftAssignableToNumber && !rightAssignableToNumber && areTypesComparable(left, right);

return leftAssignableToNumber && rightAssignableToNumber || !leftAssignableToNumber && !rightAssignableToNumber && areTypesComparable(left, right);
});
}
return booleanType;
Expand Down
14 changes: 7 additions & 7 deletions src/compiler/tsbuildPublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1704,7 +1704,7 @@ function getBuildInfo<T extends BuilderProgram>(state: SolutionBuilderState<T>,
function checkConfigFileUpToDateStatus<T extends BuilderProgram>(state: SolutionBuilderState<T>, configFile: string, oldestOutputFileTime: Date, oldestOutputFileName: string): Status.OutOfDateWithSelf | undefined {
// Check tsconfig time
const tsconfigTime = getModifiedTime(state, configFile);
if (oldestOutputFileTime < tsconfigTime) {
if (oldestOutputFileTime.getTime() < tsconfigTime.getTime()) {
return {
type: UpToDateStatusType.OutOfDateWithSelf,
outOfDateOutputFileName: oldestOutputFileName,
Expand Down Expand Up @@ -1855,7 +1855,7 @@ function getUpToDateStatusWorker<T extends BuilderProgram>(state: SolutionBuilde
}

// If an buildInfo is older than the newest input, we can stop checking
if (buildInfoTime && buildInfoTime < inputTime) {
if (buildInfoTime && buildInfoTime.getTime() < inputTime.getTime()) {
let version: string | undefined;
let currentVersion: string | undefined;
if (buildInfoProgram) {
Expand All @@ -1876,7 +1876,7 @@ function getUpToDateStatusWorker<T extends BuilderProgram>(state: SolutionBuilde
}
}

if (inputTime > newestInputFileTime) {
if (inputTime.getTime() > newestInputFileTime.getTime()) {
newestInputFileName = inputFile;
newestInputFileTime = inputTime;
}
Expand Down Expand Up @@ -1921,7 +1921,7 @@ function getUpToDateStatusWorker<T extends BuilderProgram>(state: SolutionBuilde
}

// If an output is older than the newest input, we can stop checking
if (outputTime < newestInputFileTime) {
if (outputTime.getTime() < newestInputFileTime.getTime()) {
return {
type: UpToDateStatusType.OutOfDateWithSelf,
outOfDateOutputFileName: output,
Expand All @@ -1931,7 +1931,7 @@ function getUpToDateStatusWorker<T extends BuilderProgram>(state: SolutionBuilde

// No need to get newestDeclarationFileContentChangedTime since thats needed only for composite projects
// And composite projects are the only ones that can be referenced
if (outputTime < oldestOutputFileTime) {
if (outputTime.getTime() < oldestOutputFileTime.getTime()) {
oldestOutputFileTime = outputTime;
oldestOutputFileName = output;
}
Expand All @@ -1948,7 +1948,7 @@ function getUpToDateStatusWorker<T extends BuilderProgram>(state: SolutionBuilde
usesPrepend = usesPrepend || !!(ref.prepend);
// If the upstream project's newest file is older than our oldest output, we
// can't be out of date because of it
if (refStatus.newestInputFileTime && refStatus.newestInputFileTime <= oldestOutputFileTime) {
if (refStatus.newestInputFileTime && refStatus.newestInputFileTime.getTime() <= oldestOutputFileTime.getTime()) {
continue;
}

Expand All @@ -1964,7 +1964,7 @@ function getUpToDateStatusWorker<T extends BuilderProgram>(state: SolutionBuilde
// If the upstream project has only change .d.ts files, and we've built
// *after* those files, then we're "psuedo up to date" and eligible for a fast rebuild
const newestDeclarationFileContentChangedTime = getLatestChangedDtsTime(state, resolvedConfig.options, resolvedRefPath);
if (newestDeclarationFileContentChangedTime && newestDeclarationFileContentChangedTime <= oldestOutputFileTime) {
if (newestDeclarationFileContentChangedTime && newestDeclarationFileContentChangedTime.getTime() <= oldestOutputFileTime.getTime()) {
pseudoUpToDate = true;
upstreamChangedProject = ref.path;
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ ambiguousGenericAssertion1.ts(4,15): error TS2304: Cannot find name 'x'.
ambiguousGenericAssertion1.ts(4,16): error TS1005: ')' expected.
ambiguousGenericAssertion1.ts(4,19): error TS1005: ',' expected.
ambiguousGenericAssertion1.ts(4,21): error TS1005: ';' expected.
ambiguousGenericAssertion1.ts(4,24): error TS2365: Operator '>' cannot be applied to types 'any' and '<T>(x: T) => T'.


==== ambiguousGenericAssertion1.ts (5 errors) ====
==== ambiguousGenericAssertion1.ts (6 errors) ====
function f<T>(x: T): T { return null; }
var r = <T>(x: T) => x;
var r2 = < <T>(x: T) => T>f; // valid
Expand All @@ -20,4 +21,6 @@ ambiguousGenericAssertion1.ts(4,21): error TS1005: ';' expected.
!!! error TS1005: ',' expected.
~~
!!! error TS1005: ';' expected.
~~~
!!! error TS2365: Operator '>' cannot be applied to types 'any' and '<T>(x: T) => T'.

Loading
Loading