-
-
Notifications
You must be signed in to change notification settings - Fork 201
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
No way to define required properties (typescript) #1076
Comments
This could be that you need to turn on strict mode in TypeScript |
Thanks for the tip, adding this to tsconfig.json:
and this to Test.svelte
Seems to make it work ok :) Just wondering, is there a way to specify the type of more complex properties? E.g. type P = { kind: "A"; foo: boolean } | { kind: "B"; bar: string } The closest would be this, but is not quite the same:
|
type P = { kind: "A"; foo: boolean } | { kind: "B"; bar: string }
export let optName: P = { kind: "A", foo: true }; should work |
Sorry, I meant distributed across all properties, not as an object inside a single property |
The closest I got would be this, but is not quite the same: export let kind: "A" | "B"; |
I don't think that's possible. Closest would be some sort of discriminated union but won't be applicable at the top level of the component. |
Ah shame, it would be cool if svelte supported an alternate props syntax for these cases. Something like:
or:
|
I second some of the above. I'm new to Svelte, and coming from React where I can give components generic props and use clever combinations of types and allsorts to define said props, I find myself missing this level of type safety here. Exposing an "advanced" syntax which lets you define the type of props using the full power of TypeScript would be lovely. Most of the time the "simple" syntax of typing exports is probably good enough, but every now and then we can go advanced and define complex types for our props. |
I agree with the above, not just for my own sake, but also to build easier to use component libraries for others to use. When you have a complex component that requires specific props and has some optional props, it would be nice if as soon as you start typing a component it would tell you which props it expects. |
The template tsconfig disables strict mode (why??). Enabling strict mode allows props to be marked as required by Typescript[1]. The strict flag is disabled by default, but TypeScript recommends enabling it.[2] "The strict flag enables a wide range of type checking behaviour that results in stronger guarantees of program correctness. Turning this on is equivalent to enabling all of the strict mode family options, which are outlined below. You can then turn off individual strict mode family checks as needed." [1]: https://github.com/sveltejs/svelte/issues/5183#issuecomment-662595004 [2]: https://www.typescriptlang.org/tsconfig#strict
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
With the latest version of the Svelte for VS Code extension and |
Is this IDE specific? typescript strict mode is turned on for me, but I do not get any warnings/errors regarding required props inside IntelliJ or in |
Same for me |
The intellij plugin is independent of this and maintained by someone else. |
Describe the bug
There's no way currently to define a property as required in typescript.
Logs
N/A
To Reproduce
Test.svelte
App.svelte
Expected behavior
The compiler should complain that is missing "reqName", since
undefined
is not a valid value for the exported property.Stacktraces
N/A
Information about your Svelte project:
Window 10, latest svelte, rollup
Severity
I think not being able to separate required properties from optional ones impacts the ability to do safe refactors. Also, since this would be a breaking change and the official TS support just got released I think it is better to tackle this soon rather than later (or else hide the breaking behaviour under a flag).
The text was updated successfully, but these errors were encountered: