Repository: PowerShell/PowerShell
File: src/System.Management.Automation/engine/parser/TypeInferenceVisitor.cs (C#)

Code before:
            }

            switch (binaryExpressionAst.Operator)
            {
                case TokenKind.Xor:
                case TokenKind.And:
                case TokenKind.Or:
                case TokenKind.Icontains:
                case TokenKind.Inotcontains:
                case TokenKind.Ccontains:
                case TokenKind.Cnotcontains:
                case TokenKind.Iin:
                case TokenKind.Inotin:
                case TokenKind.Cin:
                case TokenKind.Cnotin:
                case TokenKind.Is:
                case TokenKind.IsNot:
                    // Always returns a bool
                    return BinaryExpressionAst.BoolTypeNameArray;

                case TokenKind.Format:
                case TokenKind.Join:
                case TokenKind.Ireplace:
                case TokenKind.Creplace:
                    // Always returns a string
                    return new PSTypeName[] { new(typeof(string)) };

                case TokenKind.Isplit:
                case TokenKind.Csplit:
                    // Always returns a string array
                    return new PSTypeName[] { new(typeof(string[])) };

                case TokenKind.Ieq:
                case TokenKind.Ine:
                case TokenKind.Ige:
                case TokenKind.Igt:
                case TokenKind.Ilt:
                case TokenKind.Ile:
                case TokenKind.Ilike:
                case TokenKind.Inotlike:
                case TokenKind.Imatch:
                case TokenKind.Inotmatch:
                case TokenKind.Ceq:
                case TokenKind.Cne:
                case TokenKind.Cge:
                case TokenKind.Cgt:
                case TokenKind.Clt:
                case TokenKind.Cle:
                case TokenKind.Clike:
                case TokenKind.Cnotlike:
                case TokenKind.Cmatch:

Review comment (performance):
I see we already have `internal static readonly PSTypeName[] BoolTypeNameArray = new PSTypeName[] { new PSTypeName(typeof(bool)) };`. So we could cache this too.

Code after:
                case TokenKind.Cnotin:
                case TokenKind.Icontains:
                case TokenKind.Iin:
                case TokenKind.Inotcontains:
                case TokenKind.Inotin:
                case TokenKind.Is:
                case TokenKind.IsNot:
                case TokenKind.Or:
                case TokenKind.Xor:
                    // Always returns a bool
                    return BinaryExpressionAst.BoolTypeNameArray;

                case TokenKind.As:
                    // TODO: Handle other kinds of expressions on the right side.
                    if (binaryExpressionAst.Right is TypeExpressionAst typeExpression)
                    {
                        var type = typeExpression.TypeName.GetReflectionType();
                        var psTypeName = type != null ? new PSTypeName(type) : new PSTypeName(typeExpression.TypeName.FullName);
                        return new[] { psTypeName };
                    }
                    break;

                case TokenKind.Ceq:
                case TokenKind.Cge:
                case TokenKind.Cgt:
                case TokenKind.Cle:
                case TokenKind.Clike:
                case TokenKind.Clt:
                case TokenKind.Cmatch:
                case TokenKind.Cne:
                case TokenKind.Cnotlike:
                case TokenKind.Cnotmatch:
                case TokenKind.Ieq:
                case TokenKind.Ige:
                case TokenKind.Igt:
                case TokenKind.Ile:
                case TokenKind.Ilike:
                case TokenKind.Ilt:
                case TokenKind.Imatch:
                case TokenKind.Ine:
                case TokenKind.Inotlike:
                case TokenKind.Inotmatch:
                    // Returns a bool or filtered output from the left hand side if it's enumerable
                    var comparisonOutput = new List<PSTypeName>() { new(typeof(bool)) };
                    comparisonOutput.AddRange(InferTypes(binaryExpressionAst.Left));
                    return comparisonOutput;

                case TokenKind.Creplace:
                case TokenKind.Format:
                case TokenKind.Ireplace:
                case TokenKind.Join: