(Reading time: ~10 minutes)
With the grace of Allah (s), we will cover the following headings (click to navigate):
- Defining ‘ilm al-ghayb
- The problem of ‘ilm al-ghayb in the Qurʿān
- The problem of ‘ilm al-ghayb in traditions
- The scope of ‘ilm al-ghayb
- The incompatibility of ‘ilm al-ghayb with Prophets/Imāms not avoiding death
Sections 2 and 4 are extrapolated from Ayatullāh Makārim Shīrāzī’s entry on this topic from The Message of the Qurʿān  with additions from other sources. My comments will be italicized.
1 Defining ‘ilm al-ghayb
“al-ghayb is a root noun (maṣdar) [such as] ‘The sun disappeared’ and similar to [the sun] when it is concealed from the eyes.” 
In the Qurʿān, al-ghayb is often compared against its opposite, shahādah (the witnessed/seen), and is referred to as a phenomenon beyond the physical and tangible realm. It cannot be observed with the ordinary senses but requires its own set of tools for observation (idrāk).
‘Allāmah Ṭabāṭabāi (r) says:
“ghayb and shahādah are two relative concepts; that is, something can be ghayb with respect to one thing and shahādah to another. This is because all beings are limited and cannot be separated from their limitations and boundaries. Therefore, everything that falls into the [existential] boundary of another, it is witnessed with respect to that [higher] being…and if anything falls outside its existential boundaries, it will be ghayb for it” 
In this sense, we notice three different usages of al-ghayb in the Qurʿān:
- In its linguistic meaning: “Those who believe in the unseen” (2:3): those who believe in the metaphysical realities of the Divine, the hereafter, angels, etc.
- The relative meaning used in this story of Prophet Yūsuf (p): “This is so that he [al ‘Azīz] knows that I did not betray him in [his] absence (ghayb)]” (12:52)
- Matters which cannot be known through ordinary measures: “That is from the news of the Unseen which We reveal” (12:102)
2 The problem of ‘ilm al-ghayb in the Qurʿān
The following verses are explicit in the exclusivity of this knowledge to Allah (s):
- “And with Him are the keys of the Unseen; no one knows of them except Him” (6:59)
- “And say the Unseen is knowledge is only for Allah” (10:20)
- “Say [to them] ‘I do not say to you [that] with me are the treasures of God’ and ‘I do not know the unseen’” (6:50)
- “If I knew of the unseen, I would have increased in goodness and no evil would have touched me” (7:188)
However, it appears the following verses oppose this exclusivity:
- “[He] is the Knower of the unseen, He does not disclose of his unseen to anyone. Except of whom He approved from the Messengers” (72:26)
- “Nor would Allah reveal to you the Unseen. But [instead], Allah chooses of His messenger whom He wishes.” (3:179)
- “And I will inform you of what you eat and what you store in your houses” (2:49)
The last verse is in reference to Prophet Jesus (p) informing his people of private details of their lives an ordinary individual would normally have no way of telling.
We can reconcile between the two set of verses with one of the following explanations:
It is easy to see that ‘ilm al-ghayb in its absolute sense, without any boundaries or conditions, is inherently exclusive to the pure Essence of God. He has complete oversight over the Seen and Unseen realms and this knowledge is inherent to His Essence and cannot be separated from Him. But others (such as Prophets, Imāms and angels) can only gain access to the unseen secrets via divine teaching and inspiration. Therefore, what is intended by exclusivity of ‘ilm al-ghayb to God is [in its] independence; for others besides have no access to the unseen secrets independently; whatever they have is from their Lord and through His teaching, kindness and special attention.
The exclusivity in the aforementioned verses is relative – حصر إضافي – meaning, it is universally negating independent knowledge of the unseen for anyone, but it does not negate the possibility of Allah (s) teaching some of His servants of what He wishes.
In Nahj al-Balāgha, it is reported that Imam Ali (s) was giving news of future events for the people (and predicted the attack of the Mongolian Empire against Muslim lands). Upon this, one of his companions questioned whether he was relating this information from the Unseen to which he replied:
“This is not ghayb, [rather] it is a knowledge I have learnt from the Prophet”
‘Allāmah Amīni (p) adds:
“Prophets, saints and believers per the explicit verses of the Quran have access to the unseen; except, each has their own determined share; and ultimately, whatever the extent of their knowledge may be, it is limited; both from quantity and quality. Secondly, their knowledge is not essential but rather accidental; temporal and not eternal; it has a beginning and an end; and is ultimately sourced from God.”
2.2 Secured Tablet and the Tablet of Elimination and Fixing
The exclusive knowledge of Allah (s) is that of the Secured Tablet. The knowledge of Prophets and Imāms are of the tablet of maḥw (Elimination) and ithbāt (Fixing).
Both of these divine tablets are said to include all the events that have occurred and will occur until the end of time. The difference is, the events recorded in the Secured Tablet will be actualized without deterrence or change whereas information in second tablet is subject to change and update depending on the turn of events and their interaction with one another.
A blessed tradition from our third Imām points to this understanding:
لَولَا آيَةٌ فى كِتَابِ اللَّهِ لَحَدَّثْتَكُمْ بِمَا كَانَ وَ مَا يَكُونُ الى يَوْمِ الْقِيَامَةِ! فَقُلْتُ لَهُ ايَّةَ آيَةٍ؟ فَقَالَ قَوْلَ اللَّهِ يَمْحُو اللَّهُ مَا يَشَاءُ وَ يُثْبِتُ وَ عِنْدَهُ امُّ الْكِتَاب
“Were it not for a verse in the Quran I would have been able to inform you of what passed and what will happen until the day of resurrection. So I asked him which verse is this? To which he replied: “He will eliminate what He wishes and stabilizes [what He wishes] and with Him is the Mother of the Book”
2.3 Current and potential Knowledge
God concurrently is aware of all the details and events of the world whereas this is not possible for the prophets and saints. Instead, they can potentially know what they wish to know (pertinent on God’s permission).
By way of analogy, imagine a large database containing all the basic information about the citizens of your country. No person has this voluminous bank of information memorized, but government officials can access any bit of this database when they wish.
The evidence for this view can be found in Sheikh al-Kulaynī’s al-Kāfi where he lists a number of reports with the following meaning: if we wished to know [anything] we will come to know of it. 
2.4 Knowledge before and after disclosure
This view is attributed to Shahīd Mūṭahharī:
We can make a distinction here: there is a type of unseen knowledge which has been disclosed to the saints and infallibles and another type which has not been disclosed yet. In other words, ghayb has two stages: the stage before disclosure; that is, before Allah disclosed and reveals that knowledge to His messengers. In this state, no one except Him has access to this knowledge. The second stage is after disclosure. In this state, chosen individuals (“approved Messengers”) will gain access to this knowledge, and it is no longer ghayb for them. The keys of the Unseen are exclusive to God, but when He opens the doors of ghayb in a limited fashion, saints will come to know of the information made available to them. In other words, “what the infallibles had come to know were no longer technically considered ghayb [and no longer a] a contradiction to those verses.”
Perhaps the following narration is suggestive of this idea:
عَنْ أَبِي بَصِيرٍ عَنْ أَبِي عَبْدِ اَللَّهِ عَلَيْهِ اَلسَّلاَمُ قَالَ: إِنَّ لِلَّهِ عِلْمَيْنِ عِلْمٌ مَكْنُونٌ مَخْزُونٌ لاَ يَعْلَمُهُ إِلاَّ هُوَ مِنْ ذَلِكَ يَكُونُ اَلْبَدَاءُ وَ عِلْمٌ عَلَّمَهُ مَلاَئِكَتَهُ وَ رُسُلَهُ وَ أَنْبِيَاءَهُ فَنَحْنُ نَعْلَمُهُ
Reported by abī Baṣīr from Imām al-Ṣādiq: “Indeed for Allah there are two [types of] knowledge; a hidden secured knowledge [that] no ones knows of except Him. From this [knowledge] is badā. And [another type of] knowledge which he has taught the angels, messengers and Prophets; and we know of this [knowledge].”
3 The problem of ‘ilm al-ghayb in traditions
Both Sunni and Shī’ī collections of ḥadīth include a plethora of traditions citing the ability of Prophets and infallibles to access the Unseen realm. However, we also find reports with meanings contradictory to this. We will review and analyse two such reports in what proceeds:
It is reported by Sadīr that he was amongst four other companions when Imām al-Ṣādiq (s) entered with anger and said:
“[I am] surprised with a people who assume that we know of the unseen. No one knows of it except God, glory be to Him. I wished to discipline my maid…but I do not know in which of the rooms she is in!” 
In another narration found in the Amālī of Sheikh Mūfīd, ibn Mūghīrah reports that another companion related to Imām al-Kaḍim (s) that some believe the Imām possesses knowledge of the unseen. The Imām replied with surprise:
“Sūbḥānallāh! Place your hand above my head. By Allah, no hair on it or on my body remains except which has risen [from what you just said]!…[All we know] is nothing but an inheritance from the Prophet (ṣ).”
We can reread and interpret these reports with one of the following lenses:
- “These narrations are in the position of rejecting those who believed this knowledge was inherent to the Imāms and occurred without divine teaching.”
- It is possible these sayings were recorded under the environment of taqiyyah (dissimulation) either because the audience were not able to comprehend the nuances of this perplex matter or were open enemies of the Ahlulbayt (as). The first category will usually have one of two polar reactions: either reject and denounce the Imām possibly calling him blasphemous, or exaggerate and consider him divine; both of which will cast the individuals in question in sin and theological misguidance.
Intriguingly, the transmitters of the above reports both mention the presence of others in the conversation in the beginning of the report. This is itself a good sign to infer the Imām was potentially practicing caution with his words. In particular, in the first report, the Imām has a major change of tone after some of the companions who were initially with the Imām leave. Sadīr continues the report with:
“When the Imām stood up from his place and journeyed to his house, I entered [the house] with Abū Baṣīr and Muyassir and we asked him: May we be sacrificed for you, we heard you say so and so…[the report continue until it says:]… the Imām pointed to his chest and said: The knowledge of the Book, by God I swear, all of it is with us. The knowledge of the Book, by God I swear, all of it is with us.”
In affirmation of this, ‘Allāmah Ṭabāṭabāi says “People, in general, were not able to comprehend these concepts; [thus] the Prophet and Imāms would answer them in accordance to their capacity of understanding.”
4 The scope of ‘ilm al-ghayb
As we demonstrated, the possibility of ‘ilm al-ghayb for Prophets and Imāms is an established belief of our creed. However, there exists differing views with regards to its scope and degree this knowledge encompasses. We will present some of these opinions:
4.1 Comprehensive concurrent knowledge
They concurrently know of all the affairs and matters of the universe except certain pieces of information made exclusive to God Himself such as the knowledge of the time of the Day of Resurrection or knowledge of the Essence of God.
Sheikh Kulayni has mentioned six reports under the title of “The Imāms have the knowledge of what passed and what will become; and nothing is hidden from them”, which can be used as supporting evidence for this opinion.
This opinion is difficult to digest. First problem is the possibility of knowing all the details of all events that have occurred since the beginning of the world until the day of Resurrection; events at the atomic level, at the macroscopic level; mundane details of the personal lives of humans, animals, insects etc.
Second problem: Why would the Imāms whose central goal is to guide according to the Book of Allah and traditions of the Prophet need to have all this information stored in their blessed minds?Thirdly, by the law of simplicity, there are competing explanations for these traditions which require less mental effort and perplexity.
4.2 Comprehensive potential knowledge
The Imāms know of these affairs potentially, meaning, upon their desire to know they will be opened to the doors of Allah’s knowledge. This view can be fortified with previously referenced reports of “When the Imāms wish to know, they will know”.
4.3 Knowledge of general principles
They possess the general principles which govern the affairs of the universe. Upon desire to know, they derive from these principles knowledge of the specifics. For example, in a famous report it has been narrated that Imām ‘Alī (s) has said:
“Surely the Messenger of God taught me thousand doors [chapters/fields/categories] from the ḥalāl and the ḥarām from what had passed and what will come until the day of Resurrection. From each door opened another thousand, so that is a thousand thousand [one million] doors.”
Whether the number thousand is meant to be taken literally or as a way of emphasis, what is for certain is that these doors of knowledge each opened to many other doors. This is exactly the relationship between general principles and their applications. When one knows of a general rule, they can apply to a countless number of cases.
4.4 Sufficient knowledge as guides for mankid
The extent of their knowledge is defined by their roles as guides and leaders. Whatever information is relevant in giving direction to mankind, shedding light onto the confusions of their followers, leading their communities to prosperity and answering their questions will fall within the scope of their God given knowledge. This will entail complete control and mastery over the rules of God, history of Prophets and previous nations, details of creation, past and future events, human psychology, sociology, management and whatever is considered necessary or helpful in the process of guidance and leadership.
Many narrations point to this idea as well:
“God is more wise, respected, glorified, great and just to allocate a ḥujjah [for people] [and] then hide from him their affairs.”
Reported from Imām Ṣādiq (s):
“Whoever assumes that God will allocate a servant of his as ḥujjah in his lands and then will conceal from him all that he needs, surely he has attributed a lie to God.”
4.5 An amalgamation of the above
We can extend the above view in a way to include the last two:
Given the Prophet’s and Imāms’ position of guidance is universal and envelops generations of humans across history and land, with differing cultures and circumstances, and is meant to provide a source of salvation, prosperity and a safe passage of progress from this world into the hereafter for humans of all degrees of capacity, intelligence and background, we can confidently claim the breadth of the knowledge required by them in this mission is unimaginably vast. Hence, the foundational layer of their knowledge is formed by the principles taught to them by Allah (and thereon via the Prophet) to which they can refer for the rulings of specific situations. What escapes the fall of these principles either for their specificity or novelty is inspired to them by Allah when they wish and He wishes.
5 The incompatibility of ‘ilm al-ghayb with Prophets/Imāms not avoiding death
The Holy Qur’ān forbids us from casting ourselves into harms way:
“And do not throw [yourselves] with your [own] hands into destruction” (2:195)
On the other hand, history testifies that the Imāms did not attempt to escape their death. Imām Alī (s) woke up his own murderer-to-be, the cursed ibn Muljam, from sleep; Imām Ḥussain (s) travelled to Karbala conscious of his fatal end and every other Imām drank the poison of death from the hands of their enemies whilst according to your view they were aware of this by their access to the unseen knowledge. Is this not a disobedience to the prohibition mandated in the above verse?
Firstly, not all cases of facing and entering life threatening situations is prohibited. By the order of the same God who revealed the above verse, Muslims courageously participated in uneven wars, putting their lives at risk, for the sake of protecting and defending the religion and dignity of Muslims. Therefore, although human life is highly valued in God’s eyes, there are cases where it has be put on the line. If Imām Ḥussain sacrificed his life, sons, companions and allowed his women to be taken as prisoners by the cursed army of Yazīd; it was neither a miscalculation nor was it a conscious suicide mission; rather, it was fulfilling God’s orders in reviving His Book and nation, commanding the good and forbidding the evil.
Secondly, “unseen knowledge has no effect on the external world. If the Imām knew his martyrdom was an unchangeable decree of God, not preventing it is not an example of throwing oneself into destruction, rather, submitting one self to the will of God”.
“In reality, the blessed verse is forbidding of “casting” oneself unto destruction, but the events that occurred for the Imāms were a case of being “placed” in destruction not “casting”; that is, they already saw themselves in the inevitable hands of death and considered it [an] ascertained” decree of God, so they submitted to His will. This is not an area of questioning and doubt; it is an area of praise and pride; they bravely carried out God’s decree with complete consciousness of its brutality.
Lastly, Prophets and Imāms are generally commanded to act by way of appearance and face value (ẓāhir) and not by the unseen. This is why in their courts, judging amongst people, they never resorted to the unseen but rather asked for proofs, witnesses and evidence.” Otherwise, they will not be sources of imitations and role models for the rest of mankind.
اللهم اجعلنا مومنین بقاماتهم العالية
 پیام قرآن جلد 7
 Mūfradāt, al-Rāghīb v1 p616
 The maṣdar here is translated as ism maf’ūl.
 al-Mīzān, Ṭabāṭabāi v11 p418
 Nahj al-Balāgha Sermon 128
 al-Ghadīr, Āmīnī v5 p88
 Nūr al-thaqalayn, V2 P512 Hadith 160
 Uṣūl al-Kāfī v1 p285
 Collection of works, Mūṭahharī, v27 p816
 Refer to al-Ghadīr of Amīnī V5 P59-62 for an in depth discussion on these narrations
 Uṣūl al-Kāfī v1, p257
 Amāli, Sheikh Mūfīd, v1 p23
 آگاهی سوم, Ja’far Sūbḥānī, 195-197
 al-Mīzān, Ṭabāṭabāi v8 p372
 Uṣūl al-Kāfī v1, p258
 Biḥār al-anwār, v40 has reported 80 reports of similar meaning in a section named “The Messenger taught him a thousand doors”
 Biḥār al-anwār v26 p138
 al-Mīzān, Ṭabāṭabāi v18, p193
 al-Mīzān, Ṭabāṭabāi v2 p135
 al-Ghadīr, Āmīnī v1 p84-85
 Uṣūl al-Kāfī v1 p147