Allah taught him how to interpret visions as He taught him what hadiths and sincere visions are about. And this was how he survived from prison as will come in later questions..
(And so will your Lord choose you ˹O Yusuf pbuh˺, and teach you the interpretation of dreams, and perfect His favour upon you and the descendants of Yaqoub—˹just˺ as He once perfected it upon your forefathers, Abraham and Isaac. Surely your Lord is All-Knowing, All-Wise.”) Yusuf 12: 6
(The man from Egypt1 who bought him said to his wife, “Take good care of him, perhaps he may be useful to us or we may adopt him as a son.” This is how We established Yusuf pbuh in the land, so that We might teach him the interpretation of dreams. Allah’s Will always prevails, but most people do not know.) Yusuf 12:21
علمه الله تفسير الرؤى كما علمه ما تؤل إليه الأحاديث و الرؤى الصادقة. و كان ذلك ما نجاه من السجن كما سيأتي..
( وَكَذٰلِكَ يَجْتَبِيكَ رَبُّكَ وَيُعَلِّمُكَ مِن تَأْوِيلِ ٱلأَحَادِيثِ وَيُتِمُّ نِعْمَتَهُ عَلَيْكَ وَعَلَىٰ ءَالِ يَعْقُوبَ كَمَآ أَتَمَّهَآ عَلَىٰ أَبَوَيْكَ مِن قَبْلُ إِبْرَاهِيمَ وَإِسْحَاقَ إِنَّ رَبَّكَ عَلِيمٌ حَكِيمٌ)
يوسف 12: 6
(وَقَالَ ٱلَّذِي ٱشْتَرَاهُ مِن مِّصْرَ لاِمْرَأَتِهِ أَكْرِمِي مَثْوَاهُ عَسَىٰ أَن يَنفَعَنَآ أَوْ نَتَّخِذَهُ وَلَداً وَكَذٰلِكَ مَكَّنَّا لِيُوسُفَ فِي ٱلأَرْضِ وَلِنُعَلِّمَهُ مِن تَأْوِيلِ ٱلأَحَادِيثِ وَٱللَّهُ غَالِبٌ عَلَىٰ أَمْرِهِ وَلَـٰكِنَّ أَكْثَرَ ٱلنَّاسِ لاَ يَعْلَمُون)
يوسف 12: 21