Add TypeScript Examples
🙋 Documentation Request
I'd like examples of TypeScript. Please consider adding.
For examples, React Hook Form api docs offer JS and TS examples. You can switch the displayed code snippets with a button.
🧢 Your Company/Team
@devongovett Do you have any thoughts? If we implement a mechanism to display TS examples in the same way as the React Hook Form, Many users can contribute.
Of course I would like to contribute to the addition of examples.
Hey @matamatanot. I think this is something we would be happy to have in the docs but the main question would be how would it work? I think it would require some investigation to determine if it is possible with our current documentation builder/website. If you had any thoughts on this, we can discuss the options here. If it ends up proving to be a challenge, perhaps an RFC would be needed.
Has there been any update on having TS examples?
This is not something the core team is able to take on any time soon. Happy to discuss contributions!
This is not something the core team is able to take on any time soon. Happy to discuss contributions!
I'd be happy to contribute the ts examples although how easy would it be to add to the existing documentation?
I think the biggest hurdle to start with will be setting up the docs site to display and build ts examples. We have not done any investigations into this yet so I am not too sure how easy it will be.
How about, while we wait to know whether or not it's feasible in the site, to add some examples in Typescript to this repo so at least people looking for Typescript examples can find something. This would simplify the process.
Would Love to contribute some examples, I don't want to have to figure out how to integrate with your docs site however. I love typescript and react-aria however i have to say the current DX trying to get them to play nice together has been woeful. Would love to see the adobe team provide an outlet for TS dev looking to contribute TS examples
I've been working on the setup for this in #3710. That will allow us to run TSC over the examples in the docs, which are in MDX files. I've only fixed the existing errors in that PR, so once it is merged, we'd need to go through each example and add type annotations where appropriate. We'd love help with that if people are looking to contribute!
Until types in the examples are ready I have a really hard time to wrap my head around the usage of props in the existing examples. Is there some guidance on how to read the existing examples?
Example:
function CheckboxGroup(props) {
let { children, label, description, errorMessage, validationState } = props;
let state = useCheckboxGroupState(props);
let { groupProps, labelProps, descriptionProps, errorMessageProps } =
useCheckboxGroup(props, state);
What is "props' actually? I can see it has { children, label, description, errorMessage, validationState }, but shall this be read as the "complete" list of props that are supposed to be passed to CheckboxGroup?
@donaldpipowitch For this example, the "complete" list can be found in type AriaCheckboxGroupProps type. The type of props for your CheckboxGroup component is up to you and specific to what kind of API you'd like your component to have. You may want to add or remove options.
One direction you could go is to extend AriaCheckboxGroupProps, and include any additional props specific to your implementation.
Example:
import {AriaCheckboxGroupProps} from 'react-aria';
export interface CheckboxGroupProps extends AriaCheckboxGroupProps {
/* any custom props you'd like to add */
}
function CheckboxGroup(props: CheckboxGroupProps)
...
@reidbarber AriaCheckboxGroupProps is used by useCheckboxGroup, but there is also useCheckboxGroupState that gets the whole props object which uses CheckboxGroupProps. Is CheckboxGroupProps extending AriaCheckboxGroupProps? Or the other way around? Or should my interface do export interface CheckboxGroupProps extends AriaCheckboxGroupProps, CheckboxGroupProps?
@donaldpipowitch Sorry, should have picked a better name for that interface, since CheckboxGroupProps already exists in our types.
export interface MyCheckboxGroupProps extends AriaCheckboxGroupProps {
/* any custom props you'd like to add */
}
No need to extend CheckboxGroupProps from react-aria since AriaCheckboxGroupProps already extends it.
I threw some types on the example from the docs so hopefully it's a little more clear: https://codesandbox.io/s/checkbox-example-7yh5df?file=/src/App.tsx
Thank you very much @reidbarber . Is this the general rule that the "State" hooks extend from the "Aria" hooks?
Typically it is the other way around. The stately hooks usually make use of a "base" prop definition and the props for the aria hook extend from that "base", adding on aria labeling props/DOM attribute props/etc. For the component props themselves, we extend from the aria props and add style props/props to control Spectrum Design specific behavior/appearance to the type definition.
Setup work for type checking the examples is now merged! Some of the examples have been converted, but there are still lots of annotations to add. If anyone wants to help with this, we'd really appreciate PRs!
Sounds great. Do you have an example MR to show which annotations you are looking for?
For example, the useListBox examples have annotations for props going into the component. We need to expand this to all examples in the docs so that eventually we can turn on noImplicitAny in the tsconfig.
btw, if anyone is interested in helping, you can test your changes by running make check-examples to run TSC over the docs.
I think we can close this. All of the examples in the new docs website are written in TypeScript.