#Property 'bar' does not exist on type 'Record<string, any> | undefined'.

62 messages · Page 1 of 1 (latest)

lime drumBOT
#
DrWarpMan#0073

Preview:```ts
type Func = (
args: Record<string, any> | undefined
) => void

const func: Func = ({bar = "bar"}) => {
console.log(bar)
}```

rare ridge
#

Why is the error happening Property 'bar' does not exist on type 'Record<string, any> | undefined'.?

blissful swift
lime drumBOT
blissful swift
#

!ts

lime drumBOT
#
type Func = (args?: Record<string, any>) => void;


const func: Func = ({ bar = "bar" } = {}) => {
    console.log(bar)
}```
blissful swift
#

also it should be Record<string, unknown>

#

any is unsafe

blissful swift
rare ridge
blissful swift
rare ridge
#

I know, but..

#

I wanted to make a general type, for a function, that accepts 1 argument of an object of any amount of keys and any (unknown) values

blissful swift
#

typescript is javascript plus types

#

yeah but why would you need the undefined

rare ridge
#

while possibly having no object parameter at all

rare ridge
vale phoenix
#

you would need to provide a default empty object

#

args: Record<string, unknown> = {}

rare ridge
#

Okay, thanks.

rare ridge
#

args?: string
args: string|undefined same thing, no?

#

but yea, you added the default {} value, i see that

blissful swift
#

yeah

blissful swift
#

?: is an optional parameter

#

: is a required parameter that accepts undefined

#

but yeah either way, adding a default value makes the parameter optional

rare ridge
#

but I dont think I can do that with Zod 😄

jade kestrel
#

?: is an optional parameter
: is a required parameter that accepts undefined
(it acts the same way but can mean different things depending on who you ask)

rare ridge
#
z.function()
    .args(z.record(z.unknown()).optional())
    .returns(z.void());
#

gives: (args_0: Record<string, unknown> | undefined, ...args_1: unknown[])

blissful swift
jade kestrel
rare ridge
jade kestrel
#

| undefined is an explicit undefined value. e.g. foo(undefined)

#

?: is an optional parameter. e.g. foo()

rare ridge
jade kestrel
#

right, because at runtime it's the same

jade kestrel
#

oh, right, it's because it's very "general"

#

only function parameters and object attributes can be optional

#

a variable can't be optional, but can be undefined

#

so yeah, Zod decided to go for | undefined to mean both

#

you could probably write a type to turn all the | undefined to ?: in an object of function

rare ridge
#

lets not overkill 😄

blissful swift
#

typescript has real optional

#

and in fact, doesn't (or didn't) add | undefined for the longest time

#

(good ole exactOptionalPropertyTypes)

jade kestrel
blissful swift
#

oh

vale phoenix
jade kestrel
#

(right, was talking about zod after all)

blissful swift
#

does this even matter

#

can zod even do functions

jade kestrel
vale phoenix
#

eh probably should?

blissful swift
vale phoenix
jade kestrel