You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
exportasyncfunctionensurePermission<TextendsDeno.PermissionDescriptor>(descriptor: T,): Promise<Deno.PermissionStatus>{conststatus=awaitDeno.permissions.query(descriptor);if(status.state==="granted")returnstatus;awaitDeno.permissions.request(descriptor);// It would be nice to not have to use this assertion:// if ((status.state as Deno.PermissionState) === "granted") return status; // ✅ Okif(status.state==="granted")returnstatus;// ❌ Compile error: deno-ts(2367)thrownewDeno.errors.PermissionDenied("Permission denied");}asyncfunctiongreet(): Promise<void>{constvariable="USER";constdesc: Deno.EnvPermissionDescriptor={name: "env", variable };letuser: string;try{awaitensurePermission(desc);user=Deno.env.get(variable)?.trim()||"anonymous user";}catch(ex){if(exinstanceofDeno.errors.PermissionDenied)user="skeptical user";elsethrowex;}constmessage=`hello ${user}`;console.log(message);}if(import.meta.main)greet();
Note the commented lines in the function ensurePermission. If I try to run the script, I get the following compilation error:
% deno run example.ts
Check file:///Users/jesse/example/example.ts
error: TS2367 [ERROR]: This condition will always return 'false' since the types '"denied" | "prompt"' and '"granted"' have no overlap.
if (status.state === "granted") return status; // ❌ Compile error: deno-ts(2367)
~~~~~~~~~~~~~~~~~~~~~~~~~~
at file:///Users/jesse/example/example.ts:9:7
but if I comment the line in the message and uncomment the line above it, everything works fine:
% deno run example.ts
Check file:///Users/jesse/example/example.ts
⚠️ ️Deno requests env access to "USER". Grant? [g/d (g = grant, d = deny)] g
hello jesse
Is there a way that the state property/getter of PermissionStatus can be refactored in order to avoid requiring this kind of assertion or is it simply an upstream limitation of TypeScript's CFA?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Using this example:
example.ts
:Note the commented lines in the function
ensurePermission
. If I try to run the script, I get the following compilation error:but if I comment the line in the message and uncomment the line above it, everything works fine:
Is there a way that the
state
property/getter ofPermissionStatus
can be refactored in order to avoid requiring this kind of assertion or is it simply an upstream limitation of TypeScript's CFA?I am using Deno v
1.9.0
Beta Was this translation helpful? Give feedback.
All reactions