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
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.
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 withimport. When you are destructing variables, the type scope isn't changed, so when using in a type position it would either be: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.