This longitudinal study used diet-wide association studies (DWAS) to investigate the association between diverse dietary food and nutrient intakes and the onset of type 2 diabetes mellitus (T2DM). Out of 502,505 participants from the UK Biobank, 119,040 with dietary data free of T2DM at the baseline were included, and 3241 developed T2DM during a median follow-up of 11.7 years. The DWAS analysis, which is based on Cox regression models, was used to analyse the associations between dietary food or nutrient intake factors and T2DM risk. The study found that 10 out of 225 dietary factors were significantly associated with the T2DM risk. Total alcohol (HR = 0.86, 0.85–0.92, p = 1.26 × 10−32), red wine (HR = 0.89, 0.88–0.94, p = 7.95 × 10−19), and fresh tomatoes (HR = 0.92, 0.89–0.94, p = 2.3 × 10−11) showed a negative association with T2DM risk, whereas sliced buttered bread exhibited a positive association. Additionally, 5 out of 21 nutrient intake variables revealed significant associations with the T2DM risk, with iron having the highest protective effect and starch as a risk factor. In conclusion, DWAS is an effective method for discovering novel associations when exploring numerous dietary variables simultaneously and could provide valuable insight into future dietary guidance for T2DM.