D
Deno

help

Simple type narrowing/type guard/assertion (?)

Aabi5/26/2023
Is there really no simpler way of doing this without any and without external deps?
export function isFooBarBaz(
value: unknown,
): value is { Foo: { Bar: { Baz: string } } } {
return typeof value === "object" &&
value !== null &&
"Foo" in value &&
typeof value.Foo === "object" &&
value.Foo !== null &&
"Bar" in value.Foo &&
typeof value.Foo.Bar === "object" &&
value.Foo.Bar !== null &&
"Baz" in value.Foo.Bar &&
typeof value.Foo.Bar.Baz === "string";
}
export function isFooBarBaz(
value: unknown,
): value is { Foo: { Bar: { Baz: string } } } {
return typeof value === "object" &&
value !== null &&
"Foo" in value &&
typeof value.Foo === "object" &&
value.Foo !== null &&
"Bar" in value.Foo &&
typeof value.Foo.Bar === "object" &&
value.Foo.Bar !== null &&
"Baz" in value.Foo.Bar &&
typeof value.Foo.Bar.Baz === "string";
}
(What's the exact name for what I'm doing here btw? I confuse the terms.)
Mmarvinh.5/26/2023
If you want to simplify that a little bit you can drop the "KEY" in my.obj.prop checks there since the typeof expression after that takes care of that. I've seen folks using libraries like zod or @badrap/valita for building up schemas which can be used to do validate an unknown value if it matches a type.
Aabi5/26/2023
yeah, i'm actually resorting to zod now, even though i feel like i shouldn't need to :/ but i do have pretty deep and complex objects

Looking for more? Join the community!

Recommended Posts
Import is "Not yet initialized?"No idea what this means. First screenshot is error, second screenshot is problematic line, third scHow are deno processes/isolates managed in Deploy?Hi, I'm writing a multiplayer game in Deno and currently all of the game state is kept in module-scoCannot access web server from other network devices when compiledHello. Im kinda new to deno and i just compiled my first bigger project with deno, but when the taskHow to use chrome devtools / inspector with Deno?Hello all! I'd like to profile my game server written in Deno to identify the hot spots. I see thaClear local KV storeHow do I delete/clear the local database?Deno.Command not working for applicationHello. Im trying to start an application using Deno.Command, but it for some reason doesnt work. TheUint8Array or Float32ArrayIs there any significant difference between using a Float32Array and using a Uint8Array from the samSending proper Content-Type depending on file sentHello. Im new to Deno and TypeScript and i wanted to convert one of my older projects (NodeJs and JaReclaim an already registered module?Is it possible to reclaim an already registered third party module? For example if the module that iDeno.readDir on current script even if used like an http package.Hello !! I would like to do Deno.readDir and readFiles on files relative to the script that execute Top level awaits for `import()`How come to top-level awaits for `import()` not work? I found an issue on GitHub https://github.com/How to auto import on save?This doesn't work: { "deno.enable": true, "deno.lint": true, "deno.unstable": true, "editor