informed icon indicating copy to clipboard operation
informed copied to clipboard

TypeScript - Examples of using unknown types?

Open ryanmunger opened this issue 2 years ago • 4 comments

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 avatar Oct 20 '22 15:10 ryanmunger

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

joepuzzo avatar Oct 20 '22 15:10 joepuzzo

@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.

denno020 avatar Apr 10 '24 21:04 denno020

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" ?

joepuzzo avatar Apr 11 '24 13:04 joepuzzo

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?

denno020 avatar Apr 11 '24 21:04 denno020