Solution 1:

Yes, this is true for any metric spaces or more generally for any compactly generated Hausdorff spaces. A space $X$ is compactly generated if a subset $A\subseteq X$ is closed iff $A\cap K$ is closed in $K$ for all compact subspaces $K\subseteq X$. Equivalently, $X$ is compactly generated iff every map from $X$ to another space which is continuous when restricted to every compact subset of $X$ is continuous. Note that any metric space is compactly generated, since closedness of sets is detected by convergent sequences and the closure of any convergent sequence is compact.

Here is the precise statement in what seems to be the maximal generality.

Let $X$ be a compactly generated Hausdorff space and let $Y$ be a Hausdorff space. Let $f:X\to Y$ be a compact injection. Then $f$ is continuous.

To show that $f$ is continuous, it suffices to show its restriction to any compact subset of $X$ is continuous. So, let $K\subseteq X$ be compact. Then the restriction of $f$ to $K$ is a closed map, since $Y$ is Hausdorff. This means that the inverse of the restriction $g:f(K)\to K$ is a continuous bijection. Since $f(K)$ is compact and $K$ is Hausdorff, this implies $g$ is a homeomorphism. Thus $f|_K=g^{-1}$ is continuous, as desired.


Here are some quick examples to show each of the hypotheses above are needed.

  • $X$ needs to be compactly generated: If $X$ is the 1-point Lindelöfification of an uncountable discrete space and $Y$ is the same set with the discrete topology, then $X$ and $Y$ are Hausdorff and the identity map $X\to Y$ is compact but not continuous.
  • $X$ needs to be Hausdorff: If $X$ is a 2-point indiscrete space and $Y$ is a 2-point discrete space, then $X$ is compactly generated, $Y$ is Hausdorff, and a bijection $X\to Y$ is compact but not continuous.
  • $Y$ needs to be Hausdorff: If $X=[0,1]$ with the usual topology and $Y=[0,1]$ with the topology that the only nontrivial open set is $\{0\}$, then $X$ is compactly generated and Hausdorff and the identity map $X\to Y$ is compact but not continuous.