Addon-knobs: Allow text
and number
to take undefined values
#10101
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Issue:
text()
andnumber()
knobs do not allow undefined values, which become many optional props when writing React components with typescript.What I did
Update the types for each of these props to allow the value to be optional. I first tried changing
value
to optional insideKnobControlConfig
(src/components/types/types.ts
), but this threw errors in theDate
knob, and I didn't want to dive into that.How to test
This already works in knobs today when using typescript - if you pass undefined to a
text()
ornumber()
knob, you will see a typescript warning, however, if you add a//@ts-ignore
, you will notice that the knobs functions as expected with no value provided.Here is a very simple code example:
This component (
SimpleText
) takes an optional value prop, which can be a string or a number. To represent this in knobs, you can passundefined
as the value (or you may omit it as I have done in the number example). However,value
is a required property for both thenumber
andtext
functions, which will generally throw typescript errors during compilation.