-
Notifications
You must be signed in to change notification settings - Fork 69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TypeScript: WizardTaskItem
and CollapsibleBody
require a className
prop (should be optional)
#9142
Comments
@haszari do you have a PR I can look at? The problem should be fixable by making the interface WizardTaskItemProps {
// some other props…
classNames: string;
} Make the interface WizardTaskItemProps {
// some other props…
classNames?: string; // Note the `?` to make the prop optional.
} If the components are JS components it should be possible to define the types using JSDoc comments in the component files themselves: /**
* @param {string} [classNames] The classes assigned to the task item.
*/
export const WizardTaskItem = ( { /* other props */, classNames } ) => {
// …
}; This will all mean interface WizardTaskItemProps {
// some other props…
classNames: string | null; // Must provide either a string or null, not providing the prop is not an option.
}
interface WizardTaskItemProps {
// some other props…
classNames?: string | null; // string, null, or undefined allowed, not providing the prop **is an option**.
} /**
* @param {string|null} classNames Must provide either a string or null, not providing the prop is not an option.
*/
export const WizardTaskItem = ( { /* other props */, classNames } ) => {
// …
};
/**
* @param {string|null} [classNames] string, null, or undefined allowed, not providing the prop **is an option**.
*/
export const WizardTaskItem = ( { /* other props */, classNames } ) => {
// …
}; |
The JSDoc syntax supported by TS is documented here. |
That's the PR where I encountered it, but I haven't deleted the
For sure, that's what I was assuming. Adding a default param (to the JavaScript definition) fixed the bug, and seems sensible to me. @reykjalin what do you think of that fix – is it "hacky" in some way? It seems an optional bonus to define these components as TypeScript types, I'd default to leaving them implicit, but I'm curious to hear rationale for why making the types explicit would be better. |
Ah yes, that's even cleaner! I just didn't realize that was an option since it seemed based on the issue description that it should be possible to pass So long as using a default value matches the previous version of the API I see no problem with doing that 👍 |
To elaborate a bit, here's an example where it wouldn't match: // Previous. Silly example
export const WeirdComponent = ( { classNames } ) => (
<div className={ classNames }>
</div>
);
// New. Still silly example.
export const WeirdComponent = ( { classNames = 'default' } ) => (
<div className={ classNames }>
</div>
); The difference between the 2 is that the previous version allows All of this to say: make sure the default value matches the previous implementation and you should be fine! 🙂 |
Describe the bug
No merchant impact, this bugged me when working on #8926, so logging an issue.
classNames={null}
which seems redundant and unnecessary.TS2741: Property 'className' is missing in type '{ children: (false | "" | Element | null)[]; }' but required in type '{ [x: string]: any; className: any; }'.
I think this can be fixed by adding a default (e.g.
classNames = null
) to the relevant JS components (WizardTaskItem
,CollapsibleBody
).Curious for insights from TypeScript experts @Jinksi @reykjalin .
The text was updated successfully, but these errors were encountered: