Skip to content

Shortcut relations in getNarrowedTypeWorker when comparing literals #56072

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

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
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
32 changes: 31 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21118,6 +21118,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (source === target) {
return true;
}

if (relation !== identityRelation) {
if (relation === comparableRelation && !(target.flags & TypeFlags.Never) && isSimpleTypeRelatedTo(target, source, relation) || isSimpleTypeRelatedTo(source, target, relation)) {
return true;
Expand Down Expand Up @@ -28715,7 +28716,36 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
matching || type,
checkDerived ?
t => isTypeDerivedFrom(t, c) ? t : isTypeDerivedFrom(c, t) ? c : neverType :
t => isTypeStrictSubtypeOf(t, c) ? t : isTypeStrictSubtypeOf(c, t) ? c : isTypeSubtypeOf(t, c) ? t : isTypeSubtypeOf(c, t) ? c : neverType,
t => {
// WAS: isTypeStrictSubtypeOf(t, c) ? t : isTypeStrictSubtypeOf(c, t) ? c : isTypeSubtypeOf(t, c) ? t : isTypeSubtypeOf(c, t) ? c : neverType;

// Avoid going through relations for comparison between literals of the same types;
// if they're not equal then they're not related and we can skip doing a bunch of work.
if (
t.flags & TypeFlags.Literal
&& (t.flags & TypeFlags.Literal) === (c.flags & TypeFlags.Literal)
&& !(t.flags & TypeFlags.EnumLiteral && c.flags & TypeFlags.EnumLiteral)
) {
if (t.flags & TypeFlags.BooleanLiteral) {
const tIsTrue = t === trueType || t === regularTrueType;
const cIsTrue = c === trueType || c === regularTrueType;
return tIsTrue === cIsTrue ? t : neverType;
}
return (t as LiteralType).value === (c as LiteralType).value ? t : neverType;
}

if (isTypeStrictSubtypeOf(t, c)) return t;
if (isTypeStrictSubtypeOf(c, t)) return c;

// Strict subtyping is equivalent to subtyping for these types (and likely others).
// TODO(jakebailey): what other things can we quickly check here?
if (t.flags & TypeFlags.Unit && c.flags & TypeFlags.Unit) return neverType;

if (isTypeSubtypeOf(t, c)) return t;
if (isTypeSubtypeOf(c, t)) return c;

return neverType;
},
);
// If no constituents are directly related, create intersections for any generic constituents that
// are related by constraint.
Expand Down