Giter VIP home page Giter VIP logo

Comments (5)

justin-schroeder avatar justin-schroeder commented on May 28, 2024

Unfortunately I dont think typescript is able to provide suggestions for discriminated unions (which the props are).

type Props = { type: 'text' } | { type: 'select' }

I would love to be wrong on this, but until there is enough data to narrow down which union is being used I dont think the ts lsp gives us much help.

from formkit.

Lol1chw avatar Lol1chw commented on May 28, 2024

I think the problem is not with discriminated unions, but with how the type works for inputs whose type is not specified.
I mean, the following:

_: {
    type? Props['type'] extends keyof FormKitInputProps<Props>
      ? never
      : Props['type']
    value ?: string
  }

should be

_: {
        type? Props['type'] extends keyof FormKitInputProps<Props>
          ? Props['type']
          : never
        value?: string
    }

image
We get a warning if the type is there, but it's empty. But if type is not specified, then type='text' is used by default.
image
This also works for custom input, as you can see in the screenshot there is a type "myRadio"

It works like this: if Props['type'] is a FormKitInputProps key, then FormKitInputType will be equal to Props['type']. Otherwise, FormKitInputType will be equal to never.
I could be wrong, correct me if I am.

from formkit.

justin-schroeder avatar justin-schroeder commented on May 28, 2024

Oh dang. thats awesome @Lol1chw! I would definitely accept a PR with these changes, that would be a great quality of life improvement.

from formkit.

justin-schroeder avatar justin-schroeder commented on May 28, 2024

@Lol1chw this is really close. You can see there is a slight unintended consequence here: https://github.com/formkit/formkit/actions/runs/8174587760/job/22349714133

Essentially by doing this:

  _: {
    type?: Props['type'] extends keyof FormKitInputProps<Props>
      ? never
      : Props['type']
    value?: string
  }

we’ve added a value type of string to all know input types since there are now 2 type unions that can match. So something the test checking that <FormKit type="file" value="abc" /> throws a ts error no longer passes (you can run these locally by doing a build and then running pnpm test typecheck).

from formkit.

justin-schroeder avatar justin-schroeder commented on May 28, 2024

Ok, I think I got this all working now.

from formkit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.