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

TypeScript - Examples of using unknown types? #425

Open
ryanmunger opened this issue Oct 20, 2022 · 4 comments
Open

TypeScript - Examples of using unknown types? #425

ryanmunger opened this issue Oct 20, 2022 · 4 comments

Comments

@ryanmunger
Copy link

ryanmunger commented Oct 20, 2022

Are there any examples of how to use types properly using the unknown type? Struggling with getting informed v4 set up in my project.

Specifically some advice on how to type useField/useState stuff so it's not unknown:

const { fieldState, fieldApi } = useField<string, string>(props);

is what i tried, it (the compiler) accepts that line, but using fieldState.value returns an unknown type which doesn't play well with other types.

Thank you in advance!

@ryanmunger ryanmunger changed the title Typescript - Examples of using unknown types? TypeScript - Examples of using unknown types? Oct 20, 2022
@joepuzzo
Copy link
Collaborator

I personally dont use typescript but what I do know is in trying to make the types NOT unknown from informed when you get nested forms it becomes hell on earth. If some Typescript GURU wanted to come in and help me figure out better way to do it im all ears but even within my company I had a few TS people go back and forth on different solutions and never came to consensus ( ONE OF THE REASONS I HATE TS SO F$%ing much... you ask 3 devs how to type something you get three answers ) ... Ryan do you miss my energy?? lmao

@denno020
Copy link

@joepuzzo Is it possible to share a nested forms instance that we could use to hack types against?

In my projects, we're manually typing FormState to use a generic:

export type FormState<T> = {
  pristine: boolean;
  dirty: boolean;
  disabled: boolean;
  submitted: boolean;
  valid: boolean;
  invalid: boolean;
  submitting: boolean;
  validating: number;
  gathering: number;
  values: T;
  maskedValues: T;
  errors: Partial<T>;
  touched: Partial<T>;
  modified: Partial<T>;
  dirt: Partial<T>;
  focused: Partial<T>;
  initialValues: T;
  data: T;
  memory: T;
};

which works for my immediate issue, but I am by no means a TS guru. I too have been very resistant to TS, as it makes source code super ugly, but the code completion it offers is hard to ignore - and I guess the static type checking, but I find myself mostly fighting that, and only loving code completion.

@joepuzzo
Copy link
Collaborator

haha Its funny because Sooo many people I talk to share your exact same opinion on TS. Only really love it for the code completion. I share that sentiment. What do you mean exactly by "share a nested forms instance" ?

@denno020
Copy link

From your comment above:

trying to make the types NOT unknown from informed when you get nested forms it becomes hell on earth

I assume you mean something like:

<Form>
  <Form></Form>
</Form>

But wanted to make sure?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants
@ryanmunger @denno020 @joepuzzo and others