-
-
Notifications
You must be signed in to change notification settings - Fork 21
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
The required props are generated as optional #22
Comments
This is intentional, albeit a matter of taste. In our experience with porting Carbon to Svelte, it's annoying to get an error for every possible prop if it's required: <Button
icon="{undefined}"
hasIconOnly={false}
tooltipPosition="bottom"
tooltipAlignment="center"
iconDescription="{undefined}"
kind="primary"
as={false}
/>
// TSError! prop "size" cannot be undefined In your use case, is there any reason why you can't initialize a prop with a default value? |
I don't really get it. Why wouldn't you want an error if you don't pass a required prop? If it has a default value, it is optional. If it doesn't, you must pass a value. interface Props {
a?: string;
} is not the same as interface Props {
a: string | undefined;
} In the first variant, you are allowed to not pass the prop, but if you do then it must be a string (cannot be |
I think we can agree to disagree. |
@metonym in my use case the prop is a function used for generating items. There can be no default for it and the component practically doesn't make sense without it. I understand the usecase with Carbon, but perhaps this could be made as a parameter to the Sveld compiler? |
And I must agree with @aabounegm that in the case of Carbon, the typing |
@metonym I should also add that I just check the source code of the Button component in the example you gave above, and the prop is indeed optional (as it has a default value of
This way, it can be omitted but cannot be provided as |
I understand what a nullable type is. I think we differ in how we design component APIs. |
@metonym would you be interested in a PR that implements another vision behind a config option? |
@illright I'd be open to that, as long as it's opt-in |
This is important to me. I am creating some modal component that must accept unique id as prop. My usecase: Input: <script lang="ts">
export let id: string
export let closeButtonLabel: string | undefined = undefined
export let alertDialog: boolean = false
// ... Output: export interface defaultProps {
id?: undefined;
closeButtonLabel?: undefined;
/**
* @default false
*/
alertDialog?: boolean;
}
export default class extends SvelteComponentTyped<
defaultProps,
{
show: CustomEvent<any>;
hide: CustomEvent<any>;
destroy: CustomEvent<any>;
create: CustomEvent<any>;
},
{ default: {}; ["close-button"]: {}; title: {} }
> {} |
@ZerdoX-x Have you considered setting a default value that is randomly generated? export let id = "id-" + Math.random().toString(36); |
Nah. Users will need to define this id by themselves. Let's don't change my usecase. I need this prop to be required, that's it. |
@ZerdoX-x What approach do you suggest? Maybe having a |
Input: export let id: string
export let closeButtonLabel: string | undefined = undefined Expected output: id: string;
/**
* @default undefined
*/
closeButtonLabel?: string | undefined; Actual output: id?: undefined;
closeButtonLabel?: undefined; |
Currently, |
@ZerdoX-x Have you tried the following alternatives?
|
* feat: support `@required` tag to denote required prop (#22) * Run "yarn test:snapshot" * test(snapshots): add required props test case * chore: do not include `@required` tag in TS definitions * Run "yarn test:integration" * test(integration): add `@required` prop to Dropdown * Run "yarn test:integration" * docs: add `@required`
Required props are supported in v0.16.0. Use the non-standard Input <script>
/**
* This is a comment.
* @required
* @type {boolean | string}
*/
export let prop1 = true;
</script>
Output /// <reference types="svelte" />
import type { SvelteComponentTyped } from "svelte";
export interface ComponentProps {
/**
* This is a comment.
* @default true
*/
prop1: boolean | string;
}
export default class Input extends SvelteComponentTyped<
ComponentProps,
{},
{ }
> {} |
That's great, but doesn't this way clash with the Svelte way/semantics of making a prop required? In Svelte, a prop is automatically optional if it has a default value and required otherwise, so what would it mean to mark a prop with a default value as |
) * fix: uninitialized props should be required Fixes #22 * Run "yarn test:snapshot" * chore: prop is required if kind is `let` and init is `null` * breaking: remove `@required` tag * Run "yarn test:snapshot" * Run "yarn test:integration"
@aabounegm You're correct. Uninitialized |
If we have the following component:
it has a required prop
a
, but the generated prop types mark it as optional:Is this inteded behaviour? I imagine it may be misleading for the users of typings to not know which props are required.
The text was updated successfully, but these errors were encountered: