0

Consider this PermissionName type declared in lib.dom.d.ts:

type PermissionName = "geolocation" | "notifications" | // More...

Can I somehow add a value to it from my own app.d.ts file? For example (code doesn't work):

declare global {

    type PermissionName = PermissionName | "local-fonts";

}

export {}

so that this code would work (this is the reason why I cannot declare new type, because other APIs is using this type)?

await navigator.permissions.query({
    name: "local-fonts"
});

Current workaround:

await navigator.permissions.query({
    name: <any>"local-fonts"
});
Luke Vo
  • 17,859
  • 21
  • 105
  • 181
  • Does this answer your question? [Extend globally declared Type in Typescript](https://stackoverflow.com/questions/59968957/extend-globally-declared-type-in-typescript) – Tanek Loc Dec 10 '22 at 09:59
  • @TanekLoc hi, I have seen that one before. Yeah it's a duplicate though I am trying to get a newer answer in hope that there is better solution. – Luke Vo Dec 10 '22 at 12:10

1 Answers1

2

What you need to augment is not PermissionName, but navigator.permissions.query

Playground

declare global {
  interface Permissions {
    query(permissionDesc: { name: 'local-fonts' }): Promise<PermissionStatus>;
  }
}
navigator.permissions.query({
  name: "local-fonts"
});

export { }
Dimava
  • 7,654
  • 1
  • 9
  • 24
  • Ah that's very clever! I guess this is a good solution in this case although sometimes many APIs may use the same time then we have to augment that many APIs. – Luke Vo Dec 10 '22 at 15:20