Context. The determination of the abundance of volatiles in extrasolar planets is very important as it can provide constraints on transport in protoplanetary disks and on the formation location of planets. However, constraining the internal structure of low-mass planets from transit measurements is known to be a degenerate problem. Aims. Using planetary structure and evolution models, we show how observations of transiting planets can be used to constrain their internal composition, in particular the amount of volatiles in the planetary interior, and consequently the amount of gas (defined in this paper to be only H and He) that the planet harbors. We first explore planets that are located close enough to their star to have lost their gas envelope. We then concentrate on planets at larger distances and show that the observation of transiting planets at different evolutionary ages can provide statistical information on their internal composition, in particular on their volatile fraction. Methods. We computed the evolution of low-mass planets (super-Earths to Neptune-like) for different fractions of volatiles and gas. We used a four-layer model (core, silicate mantle, icy mantle, and gas envelope) and computed the internal structure of planets for different luminosities. With this internal structure model, we computed the internal and gravitational energy of planets, which was then used to derive the time evolution of the planet. Since the total energy of a planet depends on its heat capacity and density distribution and therefore on its composition, planets with different ice fractions have different evolution tracks. Results. We show for low-mass gas-poor planets that are located close to their central star that assuming evaporation has efficiently removed the entire gas envelope, it is possible to constrain the volatile fraction of close-in transiting planets. We illustrate this method on the example of 55 Cnc e and show that under the assumption of the absence of gas, the measured mass and radius imply at least 20% of volatiles in the interior. For planets at larger distances, we show that the observation of transiting planets at different evolutionary ages can be used to set statistical constraints on the volatile content of planets. Conclusions. These results can be used in the context of future missions like PLATO to better understand the internal composition of planets, and based on this, their formation process and potential habitability.