Any、unknown、object、void、undefined、null 和 never 可分配性

下表总结了一些抽象类型之间的可分配性。行表示每个可分配的内容,列表示可分配给他们的内容。"✓" 表示仅在 strictNullChecks 关闭时兼容的组合。

The following table summarizes assignability between some abstract types. Rows indicate what each is assignable to, columns indicate what is assignable to them. A "✓" indicates a combination that is compatible only when strictNullChecks is off.

anyunknown对象

1Q4jyGgv9yrKVHHMVmlgFQ==

void未定义

o+BSayCL7/UePv024aA8BA==

nullnever

9/Lm+92aG7i1zNSdWON5AUUGH0z8/Cqxm7oRuSstpLpYUeY8oLQDdISr2NCW0N0I

9cGNthKFihw5Nuqc+HC1G/kRFNkeuyjzFBJuoMkM+pM8BHu1NJs1C4dcI6HrFK0hOzeGw9Cf4vDfCeU9dwP9vA==

WXDxsZPkv9GE3SirWwFv1g==

WXDxsZPkv9GE3SirWwFv1g==

WXDxsZPkv9GE3SirWwFv1g==

WXDxsZPkv9GE3SirWwFv1g==

epleQD/NG0Zd7DfFrtjRKg==

AFKZs+F46B+t1dRoz412KVIQ3MHB+xeKUc6pB74qhdSB8/Arg6iIzGej3h1q5Ewf

WXDxsZPkv9GE3SirWwFv1g==

epleQD/NG0Zd7DfFrtjRKg==

epleQD/NG0Zd7DfFrtjRKg==

epleQD/NG0Zd7DfFrtjRKg==

epleQD/NG0Zd7DfFrtjRKg==

epleQD/NG0Zd7DfFrtjRKg==

99hxa/DvGVaLWSahWyGVxjJrWhvNDBEE1WhpISY0VETjrb2uatfiws+FMNQLDEYs

WXDxsZPkv9GE3SirWwFv1g==

WXDxsZPkv9GE3SirWwFv1g==

epleQD/NG0Zd7DfFrtjRKg==

epleQD/NG0Zd7DfFrtjRKg==

epleQD/NG0Zd7DfFrtjRKg==

epleQD/NG0Zd7DfFrtjRKg==

NNf88ZoyA6viB9E6UkRvTCOyggP2UTVZ41V5xb1soA0Vb9ULYJNhHBAh27Fqo5y6

WXDxsZPkv9GE3SirWwFv1g==

WXDxsZPkv9GE3SirWwFv1g==

epleQD/NG0Zd7DfFrtjRKg==

epleQD/NG0Zd7DfFrtjRKg==

epleQD/NG0Zd7DfFrtjRKg==

epleQD/NG0Zd7DfFrtjRKg==

FuN7esBjslcm7KgfAj2jRwFDJfOVOTOg2bY92giBM4bkxTrCGyKT7CbgA6vhDOPKlpdO9h2WvvBjyE/6jP5xoA==

WXDxsZPkv9GE3SirWwFv1g==

WXDxsZPkv9GE3SirWwFv1g==

WXDxsZPkv9GE3SirWwFv1g==

WXDxsZPkv9GE3SirWwFv1g==

WXDxsZPkv9GE3SirWwFv1g==

epleQD/NG0Zd7DfFrtjRKg==

Ml6nXK8qJsWv3aMgNdtuh6PcsQsNM/wMCbxAOoMaIA2DWx1P5KdHJJ27q1gHg8n/

WXDxsZPkv9GE3SirWwFv1g==

WXDxsZPkv9GE3SirWwFv1g==

WXDxsZPkv9GE3SirWwFv1g==

WXDxsZPkv9GE3SirWwFv1g==

WXDxsZPkv9GE3SirWwFv1g==

epleQD/NG0Zd7DfFrtjRKg==

cCI/iAk7qu0+UehcuK/kfuHai1j0ZyXi7n6AF35789aZ9j6a9Ww9lk+S/UxnBLwA

WXDxsZPkv9GE3SirWwFv1g==

WXDxsZPkv9GE3SirWwFv1g==

WXDxsZPkv9GE3SirWwFv1g==

WXDxsZPkv9GE3SirWwFv1g==

WXDxsZPkv9GE3SirWwFv1g==

WXDxsZPkv9GE3SirWwFv1g==

9yRcTiAnyEkEf5lGpWn4MN8dqR2QKiqTFn4uF2lcD0TYv2wbnOTNHz1972l13IXJGOAt8bZ6zzQ2V3nz/bo+imZmq+ZTD8TEcbIfCYcZ7aKcAnvpHYqMCy0kMOZ3I2WslF8Za0WGG/HlWorGQjXgX1L3GyHt7adGkJ/OzNvqY8ZaWxBZ/rQ0vfXPFPO/Q9RJL9yonY5SzA0KfC+HAPUA6JkqfUg0iXhT151curpghqM=

  • 一切都可以分配给它自己。

    r7WbwbAOaB/NJwwoHydGqTADwwDH7i5Qh7O0M5goXrqBXJ5nd0i1NkYsiJnhWqXa

  • anyunknown 在可分配给它们的方面是相同的,不同之处在于 unknown 不能分配给除 any 之外的任何东西。

    mGWpjVtg9yl7tt5Il+AmI3eSnbuRxec5bxm1sUcEnBYDtrNYq8y50FMhdvk9r0x2GzJ5LqyUAjs7WIvNZbuGyK3s/qBRpFJ1ljECLw3Y+sirjvIJ+ILjcxuRbnw8YP/lRW8fMTfyu6SmCfeBPAW8HnlHuCQlRmivxVo0bCzkrRndsI+UHLFkZyVLuBgd/IH1mkEFOvIDVyJ//5jCeNNlSLoS961q0yuNkbk1niLjNwpGLqy1b+n0+BQiZEK7uj+2

  • unknownnever 就像彼此的倒数。一切都可以分配给 unknownnever 可以分配给一切。没有任何东西可以分配给 neverunknown 不能分配给任何东西(any 除外)。

    j8Q64KOykXB336xYinHaKYnIVm+bwR6ZTNEMorVUXWf4Nf3f8JvHlfiitj9LqgUUhlHfq1P7vA+yc1ImOV4rXLV5tnhehGxPK0QnCEyCrQe/pEBkmJP59bBJA/1jVRxm/KVZ3Dgsx3BvJ6sek41hNfIStjbOzAJwzvSgMKYkstnzEp4O3tzNCiD1Q6spJ4ttKztCkl52D9GBGDjIW8bnFiliA6+A9Fya6CMDMbUKWYbMKd4ZNLUsW0yKawqYW/balg4pSwKLvMNcJWG0beXaPJO+uHlsvwoJRmtXvJ2zR9NQlmaWcd9DqPgbsPuoXHoR9NJMhb1sZNMpwIjoTSr0DoWE7bhOsowtei4/XFS80G5FntI9I0GM99eNJseU/P3Esb5ue32dqCRyon73NDnF2A==

  • void 不能分配给任何东西或从任何东西分配,以下例外:anyunknownneverundefinednull(如果 strictNullChecks 关闭,请参阅表格了解详细信息)。

    kZcRJCTL/lxj0i5+8+Aba9pEQRuAiwQ9GtU5bzIFAQhPdsCWqMkBwads+GoOSGB95ODCpK9TNXXPC01El7JVaErVRHRbB8KAS103WgdvE4G44H18hp2+uazIGK0RjPzZvBAfPnfHaMn2WvHhvX9VAnPFiUxOf8eKd0JuXdeLdUw0/3UQ6VbofZ116Bf8OrJ4HGDoCszi2ROE9eo+0RbfIscRc1LxwvMCJloc4KTQKVgO270dpGxT0LD3mkBow27gvK0kGnLn2DUl8rXlGZT0aYJsY9ePApmWucSNopPlVefpPcBG+3/0A7yWJQuxCOy5QB1B2a2Om0AiVUbICdO4+JSa71+7v+sYvshMGswzxREn4if72gInI2McgKDNs/V6VaYlEmyt+N3n39N8lCxOBfkVpGFK9WV0aO9bYTp+eUg=

  • strictNullChecks 关闭时,nullundefined 类似于 never:可分配给大多数类型,大多数类型不可分配给它们。它们可以相互分配。

    REJ1ZHnojQHIIdqPl2TSUH8L2e2ePGwICBeMhtIyN8vi0gwTFhlNqTRrbEpuEGQVhOcZfNIeqqJFke5EvXw/Kxvj2dcF2MkBgUprCnDNTBQiECXpy4UoF/ETMkhW4D7qjxkJr2Ybqc6HZPQd60T18hmE1Gxy6n9AIl3WDlFYUUwQDfJcR0Wcd3nw+r1UHFfPQEiMfndnmI8W37Dh/hp4OswJa0Ti4c8mp0nUi2WXdNf/5LXpjGLXWzJ1XbegB/9+LiZejWpVNQ8YKCCaTJ5i8ObTllkug+jDPFobLYBKcXE5AcSCx40aW6++92rO8zeW6ETNGbSgrd1HCDj3SKEKsHJsT9bcWN0TFYI/C6VpnuM=

  • strictNullChecks 开启时,nullundefined 的行为更像 void:除了 anyunknownnevervoidundefined 始终可以分配给 void)之外,不能分配给任何东西或从任何东西分配。

    REJ1ZHnojQHIIdqPl2TSUH8L2e2ePGwICBeMhtIyN8vi0gwTFhlNqTRrbEpuEGQVhOcZfNIeqqJFke5EvXw/Kxvj2dcF2MkBgUprCnDNTBS3vvi6epntk6ot2+ezNLPS/tnLFFDwZYF5Y74xLW1LTPZpCbMqA/f0Zup65lW2AAxh5DXL2Cv9avxmHFYE5aF4rg0BnBGduw5k/drZ2NTIfJLsTpfRpDFRJM5W9uud4nU1xDjOPeWqLxn9NZJl5IBjcp49Xa52RISKu21pZNcysA7d+6Ttz9uSZXsmTwtC/TK6rQv2dAGZL6dyZN01BiGJ6Es8+6DbAjEog1QoWIV3Qk39pBhcwsjZe6hVgw0AqPuuoTaffe6FS+LXv/+9LWDgochsFHgix1rjJ/7VHtMmu68HeSlUGimWyf1wgrZD97/kj738VVEyruTsLU3UxKmd3dEWvRxUTirvc7WV8chJmTqCLcVx7BswDtjSowtj8LJV+4c8AmIGwAunC8V1zQqK