Skip to content

Type PathValue does not work as expected with arrays #5089

@Nouchey

Description

@Nouchey

What happened?

The TypeScript utility type PathValue is supposed to extract a specific (possibly nested) value from a form's schema. It is used by a bunch of functions, such as setFieldValue().

When a form's schema TValues has some field arr that's a typed array, then 'arr[0]' is correctly assignable to Path<TValues>, but PathValue<TValues, `arr[${number}]`> resolves as never, when I'd expect it to have whatever type of array arr should contain within TValues.

never is a fallback in PathValue's nested ternary logic. I wouldn't exactly know how to fix it right now, but it definitely doesn't seem like expected behavior.

I should also note that the original React code that paths.ts copied over suffers from the same issue, even in its current state. Is this simply impossible with current TypeScript tools?

I've linked a TypeScript Playground link where the example uses an array of objects, but the issue arises with primitives as well, as suggested in my example here.

Reproduction steps

import { useForm } from 'vee-validate';

type MyFormType = {
  arr: Array<string>;
};

const { setFieldValue } = useForm<MyFormType>();

// The following works at runtime, but errors at transpilation as TypeScript expects never
setFieldValue('arr[0]', ''); // TypeScript expects never instead of a string
// I would need `as never` everywhere (i.e. after all the values I set) to avoid errors

Version

Vue.js 3.x and vee-validate 4.x

Demo link

Error example on TypeScript Playground

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions