Skip to content

Accept boolean as a value for radio input bind groups #1297

@lovasoa

Description

@lovasoa

Describe the problem

Currently, the following code:

<script lang="ts">
    export let firstSelected = true;
</script>

<input type="radio" bind:group={firstSelected} value={true}/>
<input type="radio" bind:group={firstSelected} value={false}/>

Raises the following type error:

Error: Type 'true' is not assignable to type 'string | number | string[]'. (ts)
Error: Type 'false' is not assignable to type 'string | number | string[]'. (ts)

(see https://stackoverflow.com/questions/65841762/bind-radio-group-to-a-boolean-value-using-svelte )

Describe the proposed solution

boolean could be accepted as a radio input value

Alternatives considered

A working alternative is to just let the user code their own two-way binding, but that is not very clean, and may be error-prone:

<script lang="ts">
    export let firstSelected = true;
    
    $: _firstSelected = Number(firstSelected);
    
    function handleChange(e) {
        firstSelected = Boolean(_firstSelected);
    }
</script>

<input type="radio" bind:group={_firstSelected} value={1} on:change={handleChange}/>
<input type="radio" bind:group={_firstSelected} value={0} on:change={handleChange}/>

Importance

would make my life easier

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixedFixed in master branch. Pending production release.bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions