question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Wrong TS typing of input.value when using parseValue/formatValue that convert types

See original GitHub issue

Are you submitting a bug report or a feature request?

A bug report I think

What is the current behavior?

So if I make a simple field that stores values of type A and uses some input that accepts values of type B, I get a weird type mismatch. For example, I have a field of type Date that uses a simple html select as input (please see a sandbox link below). I add parseValue that gets string from select and returns Date to store, and I add formatValue which does the opposite. In this case I would expect the input.value of FieldRenderProps to be a string but typescript infers its type as Date (in fact it’s a string). So I see a TS error.

What is the expected behavior?

I would expect the input.value of FieldRenderProps to be a string.

Sandbox Link

https://codesandbox.io/s/strongly-typed-form-values-with-react-final-form-forked-nfmmq?file=/src/index.tsx

What’s your environment?

react-final-form: 6.3.5 final-form: 4.20.1 typescript: 4.3.2

Other information

Maybe I’m doing something wrong. Would appreciate if anyone could help me to figure the things out.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
erikrascommented, Sep 29, 2021

Published fix in v6.5.4.

0reactions
mmoocommented, Aug 19, 2021

@callmeberzerker I think that UX considerations stand apart from the issue I described. Yes, your example makes sense but I’ve had a couple of real life form fields that could win from such type conversion without harming the UX. For example I use a custom DatePicker component that accepts and returns Dates but I need to submit date-only formatted strings to server. And I prefer submitting form state unchanged without running it through some pre-submit converter. This is the case where type conversion through parse/format could help.

@mrfratello wow thanks! Your PR really fixes the thing for me 😍 Too bad it is unlikely to be merged. The repo looks abandoned.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TS7053: TypeScript error when handling input id och input value
I run this in console: console.log(typeof CustomForm, 'customform type is') and it gives me a function type. I want to mention that CiustomForm ......
Read more >
How to convert a string to a number in TypeScript
I intend the user to input a number in a form field field but the value I get back is a string (even...
Read more >
Documentation - TypeScript 4.3
Separate Write Types on Properties. In JavaScript, it's pretty common for APIs to convert values that are passed in before storing them.
Read more >
15 Typescript Mistakes To Avoid - SoftwareMill Tech Blog
Conversion of type 'undefined' to type 'string' may be a mistake because neither type sufficiently overlaps with the other.
Read more >
TypeScript - how to work with implicit and explicit types?
“The first thing that many new TypeScript developers do when they convert a codebase from JavaScript is fill it with type annotations.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found