mostprodev
mostprodev17mo ago

Deno KV Type Error

When I run await (await kv.get<typeThing>(["example"])).id (the value is an object), it returns a type error because it is a maybe type thing.
7 Replies
mostprodev
mostprodevOP17mo ago
The error:
Property 'id' does not exist on type 'KvEntryMaybe<typeThing>'.
Property 'id' does not exist on type '{ key: KvKey; value: null; versionstamp: null; }'.deno-ts(2339)
Property 'id' does not exist on type 'KvEntryMaybe<typeThing>'.
Property 'id' does not exist on type '{ key: KvKey; value: null; versionstamp: null; }'.deno-ts(2339)
Perhaps the problem changed. The top is the problem. Although I check that await kv.get<typeThing>(["example"]) != null, with an if statement, it still does not get rid of the error.
Fifth-Normal-Form
let result = await kv.get<typeThing>(["example"])
// get returns a KvEntryMaybe object = {key, value, versionstamp}
// If (result.value === null) => `key was not found`
if {result.value) console.log(result.value.id);
// no error will be thrown! your error was that 'id' does not exist // on the `KvEntryMaybe` returned by the `get` method.
let result = await kv.get<typeThing>(["example"])
// get returns a KvEntryMaybe object = {key, value, versionstamp}
// If (result.value === null) => `key was not found`
if {result.value) console.log(result.value.id);
// no error will be thrown! your error was that 'id' does not exist // on the `KvEntryMaybe` returned by the `get` method.
mostprodev
mostprodevOP17mo ago
Cool. This is not different from != null.
Fifth-Normal-Form
You could do (result !== null) Be aware that the object returned by the promise KvEntryMaybe = {key, value, versionstamp}, will always return the key, but if not found, both the value, and the timestamp will be null.
mostprodev
mostprodevOP17mo ago
Oh, okay. Thanks.
Fifth-Normal-Form
Also, as someone could insert a row with the value set to null, it might be best to check the versionstamp for null, rather than the value. Be aware that some folks might just use the multipart key as both key and data, and just put null in the value field.
mostprodev
mostprodevOP17mo ago
This doesn't work.
'user_data.value' is possibly 'null'.deno-ts(18047)
(property) value: User | null
'user_data.value' is possibly 'null'.deno-ts(18047)
(property) value: User | null
Even when doing the if statement + the result declaration. Nvm.

Did you find this page helpful?