Skip to content

Generic form on T access with keyof T #2082

@ThibaudDauce

Description

@ThibaudDauce

Version:

  • @inertiajs/vue3 version: 1.2.0

Describe the problem:

When I try to access my form on a generic model T with a key: keyof T, Typescript raised an issue.

Steps to reproduce:

<template>
  <div>
    <!-- Working -->
    <div>{{ form[key] }}</div>

    <!-- Working -->
    <div>{{ model[genericKey] }}</div>

    <!-- Type 'keyof T' cannot be used to index type 'DistributeRef<InertiaForm<T>> -->
    <div>{{ genericForm[genericKey] }}</div>
  </div>
</template>

<script setup lang="ts" generic="T extends object">
import { InertiaForm, useForm } from '@inertiajs/vue3';

const props = defineProps<{
  model: T;
  genericKey: keyof T;
}>();

// Test with non generic default values
const defaultValues = { name: 'Thibaud' };
const form = useForm(defaultValues);
const key = 'name' as keyof typeof defaultValues;

const genericForm: InertiaForm<T> = useForm(props.model);
</script>

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedExtra attention is neededtypescriptRelated to TypeScript implementationvue 3Related to the vue 3 adapter

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions