{"record":{"id":"e032c8c30a5b84d4","repo":"reflex-dev/reflex","slug":"match-cases-should-have-the-same-return-types-cas","errorCode":null,"errorMessage":"Match cases should have the same return types. Case {index} with return value `{return_value._js_expr if isinstance(return_value, Var) else textwrap.shorten(str(return_value), width=250)}` of type {type(return_value)!r} is not {return_type}","messagePattern":"Match cases should have the same return types\\. Case (.+?) with return value `(.+?)` of type (.+?) is not (.+?)","errorType":"exception","errorClass":"MatchTypeError","httpStatus":null,"severity":"error","filePath":"packages/reflex-components-core/src/reflex_components_core/core/match.py","lineNumber":216,"sourceCode":"        \"\"\"\n        first_case_return = match_cases[0][-1]\n        return_type = type(first_case_return)\n\n        if isinstance(first_case_return, BaseComponent):\n            return_type = BaseComponent\n        elif isinstance(first_case_return, Var):\n            return_type = Var\n\n        cases = []\n        for index, case in enumerate(match_cases):\n            conditions, return_value = case\n            if not isinstance(return_value, return_type):\n                msg = (\n                    f\"Match cases should have the same return types. Case {index} with return \"\n                    f\"value `{return_value._js_expr if isinstance(return_value, Var) else textwrap.shorten(str(return_value), width=250)}`\"\n                    f\" of type {type(return_value)!r} is not {return_type}\"\n                )\n                raise MatchTypeError(msg)\n            cases.append((conditions, return_value))\n        return cases\n\n    @classmethod\n    def _create_match_cond_var_or_component(\n        cls,\n        match_cond_var: Var,\n        match_cases: list[tuple[list[Var], BaseComponent]]\n        | list[tuple[list[Var], Var]],\n        default: Var | BaseComponent | None,\n    ) -> Component | Var:\n        \"\"\"Create and return the match condition var or component.\n\n        Args:\n            match_cond_var: The match condition.\n            match_cases: The list of match cases.\n            default: The default case.\n","sourceCodeStart":198,"sourceCodeEnd":234,"githubUrl":"https://github.com/reflex-dev/reflex/blob/45b8ed5ab735f8a56bbb09a42384f030eb0208e7/packages/reflex-components-core/src/reflex_components_core/core/match.py#L198-L234","documentation":"rx.match requires all case return values (and the default) to share the same type so it can produce a well-typed var. This MatchTypeError fires when one case's return value type differs from the first case's.","triggerScenarios":"e.g. rx.match(State.n, (1, \"one\"), (2, 2), \"other\") — mixing str and int returns. Also mixing Var-typed returns with literal returns of a different _var_type.","commonSituations":"Returning a component in one case and a string in another; mixing a State var of type int with literal strings; forgetting the default value counts toward the type check.","solutions":["Make all return values (including the default) the same type","Convert mixed literals with Var.create(value, _var_type=...) or wrap strings/numbers consistently","If mixing components and strings is intended, wrap strings in rx.text so all returns are components"],"exampleFix":"# before\nrx.match(State.n, (1, \"one\"), (2, 2), \"other\")\n# after\nrx.match(State.n, (1, \"one\"), (2, \"two\"), \"other\")","handlingStrategy":"validation","validationCode":"types = {type(c[-1]).__name__ if not isinstance(c[-1], Var) else str(c[-1]._var_type) for c in cases}\nassert len(types) == 1, f'mixed return types: {types}'\n# include the default in the check","typeGuard":"def same_return_type(cases: list[tuple], default: Any) -> bool:\n    first = type(cases[0][-1])\n    return all(type(c[-1]) is first for c in cases) and type(default) is first","tryCatchPattern":null,"preventionTips":["Keep every case return value and the default the same type","Wrap strings in rx.text when mixing with component returns"],"tags":["reflex","match","type-mismatch"],"backgroundTag":"type-mismatch-in-union","analyzedSha":"45b8ed5ab735f8a56bbb09a42384f030eb0208e7","analyzedAt":"2026-08-28T19:25:27.644Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}