Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Property 'key' does not exist on type 'IssuePathItem' #694

Closed
zougari47 opened this issue Jul 4, 2024 · 2 comments
Closed

Property 'key' does not exist on type 'IssuePathItem' #694

zougari47 opened this issue Jul 4, 2024 · 2 comments
Assignees
Labels
enhancement New feature or request workaround Workaround fixes problem

Comments

@zougari47
Copy link

IDE tell me that Property 'key' does not exist on type 'IssuePathItem'.. But when I log it it gives me the expected value.(in this case "message")
Here is the snippet

function validate() {
    const schema = v.object({
      name: v.pipe(v.string(), v.nonEmpty('Name is required')),
      email: v.pipe(v.string(), v.trim(), v.email('Invalid Email')),
      message: v.custom(
        input => typeof input === 'string' && input.split(' ').length > 5,
        'Please write a bit more so I can understand you better!'
      ),
    })

    const name = nameInput!.value ?? ''
    const email = emailInput!.value ?? ''
    const message = messageInput!.value ?? ''

    const result = v.safeParse(schema, { name, email, message })

    console.log(result)

    if (!result.success) {
      result.issues.map(issue => {
        console.log(issue.path![0].key) // 'message'
        if (issue.path && issue.path[0].key === 'message') { // here it give me the error for .key
          // show message
        }
      })
    }

    return true
  }
@fabian-hiller
Copy link
Owner

This is because not every path item has a .key property, but I plan to improve the developer experience on this part by adding key: undefined in this case. There is a workaround to fix the TS error. Here is an example:

if (issue.path && 'key' in issue.path![0] && issue.path![0].key === 'message') {
  // show message
}

Tip: You can also pass the issue to v.getDotPath(issue) to get its path.

@fabian-hiller fabian-hiller self-assigned this Jul 4, 2024
@fabian-hiller fabian-hiller added enhancement New feature or request workaround Workaround fixes problem labels Jul 4, 2024
@fabian-hiller
Copy link
Owner

v0.36.0 is available

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request workaround Workaround fixes problem
Projects
None yet
Development

No branches or pull requests

2 participants