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

interface does not satisfy the constraint FieldValues #2

Closed
kkharji opened this issue Nov 13, 2022 · 7 comments
Closed

interface does not satisfy the constraint FieldValues #2

kkharji opened this issue Nov 13, 2022 · 7 comments
Assignees
Labels
bug Something isn't working enhancement New feature or request

Comments

@kkharji
Copy link

kkharji commented Nov 13, 2022

Hello @fabian-hiller, very promising library and awesome documentation, thank you.

I'm using auto-generated interfaces for my types, but unfortunately I ran into an error:

Type 'UserAuthLogin' does not satisfy the constraint 'FieldValues'.
Index signature for type 'string' is missing in type 'UserAuthLogin'.

The interface:

export interface UserAuthLogin { login: string, password: string }

Thanks

@fabian-hiller
Copy link
Owner

Hey @kkharji, thank you for using Modular Forms! That means a lot to me. I am aware of this problem. If you use a type instead of an interface, it works.

The problem is that a form can be nested arbitrarily and I use a recursive type for that. Why TypeScript doesn't then accept an interface as generic, I don't know yet. If you are familiar with TypeScript, you can try to help me here. Otherwise I will ask for advice in the SolidJS community.

@fabian-hiller fabian-hiller self-assigned this Nov 13, 2022
@fabian-hiller fabian-hiller added bug Something isn't working enhancement New feature or request labels Nov 13, 2022
@kkharji
Copy link
Author

kkharji commented Nov 13, 2022

Oh that sucks, I was hoping it was unknown issue. Yes, typescript advance type is hard to get right, let alone supporting interface keyword.

No issues hopefully the rspc generator would be able to generate with type keyword.

Thanks @fabian-hiller

@fabian-hiller
Copy link
Owner

I am sure there is a solution to this problem. I will try to fix it tomorrow. I will give you an update here.

@fabian-hiller
Copy link
Owner

As described in this issue, the "problem" has existed in TypeScript for a very long time now. I am not yet aware of a user-friendly workaround that does not compromise type safety. I will let you know as soon as I find a solution.

@fabian-hiller
Copy link
Owner

Until TypeScript gives us a "better" solution, I recommend using type instead of interface. As an alternative, the workaround of giving an interface an explicit index signature can be used. Below are two examples.

interface LoginForm {
  email: string;
  password: string;
  [name: string]: string;
}
interface LoginForm extends Record<string, string> {
  email: string;
  password: string;
}

This may affect the editor support, so that e.g. autocomplete works only conditionally.

If someone here knows TypeScript very well and thinks he or she can solve the problem by making changes to Modular Forms, I have prepared a minimal TypeScript playground for experimenting. I'm temporarily closing the issue until there are signs that changes to our code can fix the problem.

@vchirikov
Copy link

I recommend using type instead of interface

I force to use interfaces with "@typescript-eslint/consistent-type-definitions": "error" and
extends Record<string, string> doesn't work with boolean fields for example, so it's better to use something like this:

interface LoginForm extends FieldValues {
  email: string;
  password: string;
} 

@fabian-hiller
Copy link
Owner

All solutions I know so far to be able to support interfaces reduce type safety. I have again experimented a bit, but have not come to a satisfactory solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants