A
arktype3mo ago
PIat

ParseError: undefined must be a PropertyKey or stringifyNonKey must be passed to options

Hello! When key isn't provided in the .map function, the above error is thrown. Not working code:
const mapTypes = <T extends typeof typee>(t: T) =>
t.map((prop) => {
const key = prop.key
const value = prop.value

if (value.extends(type.number)) {
return [prop, { value: value.or('string.numeric.parse') }]
}

if (value.extends('boolean')) {
return [prop, { value: value.or(['string', '=>', (v) => v === 'on']) }]
}

return prop
})
const mapTypes = <T extends typeof typee>(t: T) =>
t.map((prop) => {
const key = prop.key
const value = prop.value

if (value.extends(type.number)) {
return [prop, { value: value.or('string.numeric.parse') }]
}

if (value.extends('boolean')) {
return [prop, { value: value.or(['string', '=>', (v) => v === 'on']) }]
}

return prop
})
Working code (simply added key to return object):
const mapTypes = <T extends typeof typee>(t: T) =>
t.map((prop) => {
const key = prop.key
const value = prop.value

if (value.extends(type.number)) {
return [prop, { key, value: value.or('string.numeric.parse') }]
}

if (value.extends('boolean')) {
return [
prop,
{ key, value: value.or(['string', '=>', (v) => v === 'on']) },
]
}

return prop
})
const mapTypes = <T extends typeof typee>(t: T) =>
t.map((prop) => {
const key = prop.key
const value = prop.value

if (value.extends(type.number)) {
return [prop, { key, value: value.or('string.numeric.parse') }]
}

if (value.extends('boolean')) {
return [
prop,
{ key, value: value.or(['string', '=>', (v) => v === 'on']) },
]
}

return prop
})
Is there a reason for this behavior? I would expect the key to already be present on the prop
11 Replies
ssalbdivad
ssalbdivad3mo ago
It doesn't seem like you have the latest code since in the new version, .map expects an object as a return type Oh nevermind Oh wait Yeah that doesn't look right You're adding multiple props of the same key? What was the error you got? I'd expect them to intersect and if they're incompatible you'd get an error There should really only ever be one mapped prop per key So what you'd want instead of:
return [prop, { key, value: value.or('string.numeric.parse') }]
return [prop, { key, value: value.or('string.numeric.parse') }]
is:
return {...prop, value: prop.value.or('string.numeric.parse') }
return {...prop, value: prop.value.or('string.numeric.parse') }
It's unfortunate that return doesn't just error because it shouldn't match the mapped type It does seem like there's lots of type errors for me haha
PIat
PIatOP3mo ago
It was ParseError: undefined must be a PropertyKey or stringifyNonKey must be passed to options I see, thank you! I was just going off the example in https://discord.com/channels/957797212103016458/1114601623285678192/1285571762582458399 This mapping will make it really straightforward to automatically add the constraints to fields through Conform POC for default values:
const defaultValue = useMemo(() => {
const res = {}

def.distribute((branch) => {
branch.props.forEach((prop) => {
res[prop.key] = defaultValueExternal[prop.key] ?? prop.default
})
})

return res
}, [])
const defaultValue = useMemo(() => {
const res = {}

def.distribute((branch) => {
branch.props.forEach((prop) => {
res[prop.key] = defaultValueExternal[prop.key] ?? prop.default
})
})

return res
}, [])
ssalbdivad
ssalbdivad3mo ago
Yeah that looks a lot easier! It's even nicer if you're mapping a known set of keys because then you get strict inference for the defaults, but even if you are doing it generically and have to cast it still makes the runtime part of it a lot easier
PIat
PIatOP3mo ago
Yessssss And it works 🤩
PIat
PIatOP3mo ago
No description
No description
PIat
PIatOP3mo ago
Really, it's magical
ssalbdivad
ssalbdivad3mo ago
Looks clean!
PIat
PIatOP3mo ago
Now just to make the constraints + polish out the mapping of different form values, and Conform will be Conquer
ssalbdivad
ssalbdivad3mo ago
Amazing! Then you'll publish it as an ecosystem library so that everyone can use it right? 🙏
PIat
PIatOP3mo ago
That's the plan!!! I'm excited making forms could really be this intuitive
ssalbdivad
ssalbdivad3mo ago
Really excited to see that come to fruition!
Want results from more Discord servers?
Add your server