You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Generic type parameter in a component is not properly constrained, allowing any type to be passed when using the component, potentially leading to type safety issues. This allows invalid types to be passed without triggering type errors.
Expected Behavior
The generic type parameter should be properly constrained
Enforced type safety when using the component
Invalid types should trigger type errors during development
This is a TS-specific issue and should be caught by TS, not Svelte. And it is, for example do npm run check in this repro.
Svelte itself does not (and should not) catch any type errors, you can also see that you can pass a number to a prop of type string.
If you have the Svelte extension enabled in your IDE, it should also catch the type-error, provided it's working correctly (try putting a let a: number = "abc" in your Svelte component to check this). If it's not, that's an issue for https://github.com/sveltejs/language-tools.
Note, though REPL supports TS, the typings is ignored.
The problem is that GroupToggleItem infers T from the type of item, but in your code they are literals, just strings and not Theme.
You can type the component via const ThemeToggleItem = GroupToggleItem<Theme>
What you are describing here is not possible to achieve without helping TypeScript understand what type GroupToggleItem should receive - there's no connection on a type level here. What @7nik just described is a valid way to achieve this - there's no other way than "helping" TypeScript understand what the generic relation ship is here. Therefore closing.
Describe the bug
Generic type parameter in a component is not properly constrained, allowing any type to be passed when using the component, potentially leading to type safety issues. This allows invalid types to be passed without triggering type errors.
Expected Behavior
Reproduction
https://svelte.dev/playground/0aaba3651e6b4cf183a0cf73d539dbb4?version=5.19.4
(same behaviour in VSCode with the extension "Svelte for VS Code")
Logs
System Info
Severity
annoyance
The text was updated successfully, but these errors were encountered: