Skip to content
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 contructor's props should be required if they contain a required prop #1578

Open
ivanhofer opened this issue Aug 7, 2022 · 4 comments
Open
Labels

Comments

@ivanhofer
Copy link
Contributor

ivanhofer commented Aug 7, 2022

I have updated the description

Description

When using the JavaScript API to instantiate a Svelte component, I would find it useful to get an error if I forget to pass props.

image

In this case the Component has a required prop called title that expects a string.
I can completely omit the props attribute and won't get an error.

Proposed solution

Type the params object of the ComponentConstructorOptions to get errors when instantiating a component incorrectly.

Alternatives

No response

Additional Information, eg. Screenshots

No response

@dummdidumm
Copy link
Member

Can you give a reproducible? Not sure what exactly is going on there.

@ivanhofer
Copy link
Contributor Author

Sorry, I thought this is a general issue since I never saw it working.

I tried to create a fresh repo but cannot reproduce it there. Then I have deleted my node_modules folder and installed all dependencies again and now it works there too. It was a project that was living on my laptop a few months and I did a couple of npm updates. Maybe something broke there. But still weird..

The props are typed correctly.

Anyhow, I still don't get an error if I don't set the props attribute at all.
https://stackblitz.com/edit/vitejs-vite-regq6z?file=src/main.ts
I would expect to get an error there.

I was thinking about having type definitions like this:

interface ComponentConstructorOptionsWithoutProps {
	target: Element | ShadowRoot;
	anchor?: Element;
	context?: Map<any, any>;
	hydrate?: boolean;
	intro?: boolean;
	$$inline?: boolean;
}

interface ComponentConstructorOptionsWithProps<Props extends Record<string, any> = Record<string, any>> extends ComponentConstructorOptionsWithoutProps {
	props: Props;
}

type ComponentConstructorOptions<Props extends Record<string, any> | never = never> = Props extends never
	? ComponentConstructorOptionsWithoutProps
	: ComponentConstructorOptionsWithProps<Props>;

@ivanhofer ivanhofer changed the title the contructor's props should be typed correctly the contructor's props should be required if they contain a required prop Aug 8, 2022
@dummdidumm
Copy link
Member

dummdidumm commented Aug 8, 2022

Mhm right, the props not set at all issue is a bummer. It's the type definitions that need to be enhanced here, although that's probably more on the Svelte typings itself, and since it's a breaking change that's a v4 thing. We could add similar typings to the existing shims though (it would be breaking there, too, but with the new transformation incoming we need to do one anyway).

@ivanhofer
Copy link
Contributor Author

Yes I was also assuming that this would be a breaking change. Not sure if I understood it correctly but the language tools could "patch" the types without the Svelte-typings to change right?

Ah, and what I forgot in the example above is the following type:

interface ComponentConstructorOptionsWithOptionalProps<Props extends Record<string, any> = Record<string, any>> extends ComponentConstructorOptionsWithoutProps {
	props?: Props;
}

So in the end there should be three cases:

  • component has no props
  • component has only optional props
  • component has at least one required prop

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants