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

Provide type-safe values to <option /> when the parent <select /> has its value bound by a typed variable. #2298

Open
victormihalache opened this issue Feb 22, 2024 · 0 comments
Labels
feature request New feature or request

Comments

@victormihalache
Copy link

Describe the problem

The following component

<script lang="ts">
  let typedVariable = $state("value1");
  // If not using runes: `let typedVariable = "";`
</script

<select bind:value={typedVariable}>
  <option value="value1">Value 1</option>
  <option value="value2">Value 2</option>
</select>

should not have any errors. If I were type out the possible values however, the following gives an error, as it should:

<script lang="ts">
  let typedVariable = $state<"value1" | "value2">("value3");
  // If not using runes: `let typedVariable: "value1" | "value2" = "value3";`
</script

<select bind:value={typedVariable}>
  <option value="value1">Value 1</option>
  <option value="value2">Value 2</option>
</select>

But so should this (putting an <option /> element with an illegal value:

<script lang="ts">
  let typedVariable = $state<"value1" | "value2">("value1");
  // If not using runes: `let typedVariable: "value1" | "value2" = "value1";`
</script

<select bind:value={typedVariable}>
  <option value="value1">Value 1</option>
  <!-- Putting illegal value here -->
  <option value="value3">Value 3</option>
</select>

Describe the proposed solution

It would be nice is the svelte compiler would be able to warn you when the bind directive is used with a value and its type is not respected in this case.

The following are the edge-cases I identified where there could be type checking:

  1. When an illegal option is used
<script lang="ts">
  let typedVariable = $state<"value1" | "value2">("value1");
  // If not using runes: `let typedVariable: "value1" | "value2" = "value1";`
</script

<select bind:value={typedVariable}>
  <option value="value1">Value 1</option>
  <!-- Putting illegal value here -->
  <option value="value3">Value 3</option>
</select>
  1. When a loop includes illegal values
<script lang="ts">
  let typedVariable = $state<"value1" | "value2">("value1");
  // If not using runes: `let typedVariable: "value1" | "value2" = "value1";`

  let values = ["value1", "value3"]
</script

<select bind:value={typedVariable}>
  <!-- Should say that `values` is not assignable to `(typeof typedVariable)[]`  -->
  {#each values as value}
    <option value={value}>{value}</option>
  {/each}
</select>
  1. Objects — Although I haven't thought much about this just yet, some type checking to see if the type of the value being put inside the value attribute is "correct" based on the type of the parent's bound variable should be done.

Importance

nice to have

@dummdidumm dummdidumm transferred this issue from sveltejs/svelte Feb 22, 2024
@dummdidumm dummdidumm added the feature request New feature or request label Feb 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants