-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Introduce $typed() rune, complimentary to $bindable() #14844
Comments
IMO, it seems like it would be a good compromise and, without necessarily encouraging its usage, let people who prefer this syntax use it instead. Also, this would be completely compiled away so no change in the generated code.
UPDATE: I think I think it could also only "exist" when declaring props and only if the code block is typescript. Otherwise, svelte can disable the usage of this rune, e.g. show error / warning, e.g. |
Personal opinion aheadI don't think this should be added to svelte for various reasons:
Example userspace implementation function $typed<Type>(): undefined;
function $typed<Type>(def: Type) : Type;
function $typed(def?: any) {
return def;
}
const a = $typed<"a" | `n-${number}`>();
const b = $typed<"a" | `n-${number}`>("b"); //Error
const n = $typed<"a" | `n-${number}`>("n-1"); |
Then those people won't use it but if they happen to use it, it's completely harmless. The documentation also helps. In addition, if the code block is not typescript, svelte can disable the usage of this rune, e.g. show error / warning, e.g.
I don't see this is a problem, if you don't use it, you don't see it. The change is pretty trivial and it adds zero code to the output, as it's completely compiled away, and thus it can't cause any bugs.
This or anything created in user-land doesn't work with |
let {
size = 'md',
color,
value = $bindable()
}: {
/**
* Sets the desired size of the component.
*/
size?: 'sm' | 'md' | 'lg',
/**
* Sets the desired color of the component's text.
*/
color: Color,
/**
* Sets the component's displayed value.
*/
value: string
} = $props() How would you allow JsDoc? All our properties for all components in our component library carry these as they show up in Intellisense, making the use of the components significantly easier. |
That wouldn't change since you're not defining the types "inline". You wouldn't use this rune at all. This rune is only needed if you wanted to define types inline: let {
size = $typed<'sm' | 'md' | 'lg'>('md'), // optional
color = $typed<Color>(), // required
value = $bindable<string>()
} = $props(); |
Describe the problem
As explained in #14810 and #9241, using $props rune is inconvenient with TypeScript.
Describe the proposed solution
There is already a typed
$bindable
rune that can be used for inline types. We could introduce a$typed
(non-bindable) rune to be able to specify fallback value and type at the declaration site of the prop, not separately:instead of
Importance
would make my life easier
The text was updated successfully, but these errors were encountered: