Deno: TypeScript errors when accessing DenoError

Created on 19 Feb 2019  路  4Comments  路  Source: denoland/deno

The following crashes in v0.3.0:

const { DenoError } = Deno;
const e = {};
e as DenoError;
console.log("ok");

With the following error

> deno tests/deno_error.ts
Compiling file:///Users/rld/src/deno/tests/deno_error.ts
/Users/rld/src/deno/tests/deno_error.ts:3:6 - error TS2304: Cannot find name 'DenoError'.

3 e as DenoError;
       ~~~~~~~~~

cc @kitsonk
ref https://github.com/denoland/deno_std/pull/205

bug

Most helpful comment

When importing the local type scope matches the variable scope, so import { DenoError } from "deno"; works. That is because there is an implied import/export of the type scope with import. When you are destructing variables, the type scope isn't changed, so when using in a type position it would either be:

e as Deno.DenoError;
// or
type DenoError = Deno.DenoError;

While I don't know the whole reasoning, is if the type scope changed every time you created a variable, the type scope would become unwieldy. I haven't looked to see if there is an issue for this in TypeScript, but I suspect they wouldn't have a good reason to change that behaviour though.

All 4 comments

This is because const pulls the value DenoError out, but not the corresponding type. You鈥檇 have to do type DenoError = Deno.DenoError or e as typeof DenoError instead.

But the following works:

// x.ts
import { C } from "./c.ts";
const x = {};
x as C;
console.log("ok");
// c.ts
export class C { }



md5-9da0b6008544f5f894fc702c22c43917



> deno x.ts
Compiling file:///Users/rld/x.ts
ok

When importing the local type scope matches the variable scope, so import { DenoError } from "deno"; works. That is because there is an implied import/export of the type scope with import. When you are destructing variables, the type scope isn't changed, so when using in a type position it would either be:

e as Deno.DenoError;
// or
type DenoError = Deno.DenoError;

While I don't know the whole reasoning, is if the type scope changed every time you created a variable, the type scope would become unwieldy. I haven't looked to see if there is an issue for this in TypeScript, but I suspect they wouldn't have a good reason to change that behaviour though.

DenoError has been replaced with specified error classes in #3936 so this issue is no longer valid.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

CruxCv picture CruxCv  路  3Comments

kitsonk picture kitsonk  路  3Comments

motss picture motss  路  3Comments

justjavac picture justjavac  路  3Comments

doutchnugget picture doutchnugget  路  3Comments