Comments (5)
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.
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
}
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.
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.
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.
@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.
Ok, I think I got this all working now.
from formkit.
Related Issues (20)
- Listbox and deletion issues with autocomplete, dropdown, and taglist at FormKit Pro v0.125.0 HOT 2
- Prefix Icon not aligned in Datepicker HOT 2
- Multi-input validation fails to update when there are multiple rules
- Missing loading-state when using `multiple` and `optionLoader` on Autocomplete
- DatePicker selects disabled date by default HOT 6
- Not able To pass Validation condition On input Field
- When a dropdown field that's rendered before fetching the option is pre-filled, it displays the value instead of the label HOT 3
- Hovering dates in datepicker is very slow HOT 2
- Setting initial multi step activeStep does not work HOT 1
- LocalStorage plugin does not preserve uploaded files HOT 1
- Transferlist has accessibility issues. Can be seen by using dev tools lighthouse HOT 1
- Unable to use slots in custom components HOT 2
- Currency field doesn't fire dom-input-event on user's input
- Currency input - label slot not recognised by TS HOT 1
- Field IDs beginning with numbers not valid HOT 5
- Validation not working in currency input HOT 1
- Validation stop firing when custom rule used together with another rule (don't know if just in the described context) HOT 1
- Transfer List Input - Search should be implemented on target list HOT 1
- Form inputs marked as invalid after touch when resetting form HOT 2
- multi-step label attribute don't support i18n HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from formkit.