簡體   English   中英

打字稿錯誤-類型上不存在屬性“權限”

[英]Typescript error - Property 'permission' not exists on type

我有這個JavaScript代碼,用於顯示通知權限的當前狀態:

main.js

var $status = document.getElementById('status');

if ('Notification' in window) {
    $status.innerText = Notification.permission;
}

的index.html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <p>Current permission status is
        <b id="status">unavailable</b>
    </p>
    <script src="/scripts/main.js"></script>
</body>

</html>

如果我在打字稿文件中寫入相同的代碼,則會出現此錯誤:

main.ts

var $status = document.getElementById('status');

if ('Notification' in window) {
    $status.innerText = Notification.permission;
}

ERROR - Notification.permission

MESSAGE - Property 'permission' not exists on type
          'new(title: string, options?: NotificationOptions): Notification;
          prototype: Notification;
          requestPermission(callback?: NotificationPermissionCallback): 
          Promise<string>;'

如何忽略此錯誤?

嘗試將Notification強制轉換為any類型,以避免編譯器錯誤。

if ('Notification' in window) {
    $status.innerText = (Notification as any).permission;
}

另一個選項是包括Notification類型的定義。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM