microsoft / microsoft/TypeScript

Type PermissionName for Permission API doesn't contain microphone and camera devices

Open
#59,805 2 comments 1 reaction 1 assignee View on GitHub

@sandersn is already working on this.

Since Aug 30, 2024.

Needs Investigation
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

🔍 Search Terms

microphone, camera, device, permission, state, API, navigator

✅ Viability Checklist
⭐ Suggestion

In both files, webworker.generated.d.ts and dom.generated.d.ts in line 9361 and 27992.

The modification would be add two terms in the type PermissionName. 'camera' and 'microphone' to check access in both devices

📃 Motivating Example
  const checkForPermission = (type: PermissionName) => {
    navigator.permissions
      .query({
        name: type,
      })
      .then(permissionStatus => {
        // Will be 'granted', 'denied' or 'prompt':
        console.log(permissionStatus);
        // permissionStatus.state = 'granted'
        // Listen for changes to the permission state
        permissionStatus.onchange = () => {
          console.log(permissionStatus.state);
        };
      });
  };

  checkForPermission('microphone');
  checkForPermission('camera');

Expected behavior:
'microphone' and 'camera' should be valid types for PermissionName type.

Actual behavior:
'microphone' and 'camera' are not valid types for PermissionName type.
Typescript is showing error:
Argument of type '"microphone"' is not assignable to parameter of type 'PermissionName'.

💻 Use Cases
  1. What do you want to use this for?
    Check if camera or microphone are enabled/disabled

  2. What shortcomings exist with current approaches?
    To type the promise from navigator.permission in typescript, we have to add a inclusion on type PermissionName like: { PermissionName } & { name: 'camera' | 'microphone' }.

  3. What workarounds are you using in the meantime?
    I'm using it: { PermissionName } & { name: 'camera' | 'microphone' }

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.