Skip to content

withDefaults shows error when using generic #8310

Closed
@pikax

Description

Vue version

3.3.2

Link to minimal reproduction

https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAbzgEwKYDNgDtUAUoRgDOANHAO7AwAWAIhgIYCuANjEXAL5zoEhwByAG5NUAgFDj0TLAGMYwCFh4MA1qgDCEcEtRYYAHgAqcVAA8Ye5ByIwo2AOZwAPnCysWAPgAUASkTicEFwUKgwTFDKlDT06MxsRN5omDj4hEQGCIHBOZa2AFxwRtlBnD6+ZFk5uagFcH5wALyegnkwEjmcvuKc4kA

Steps to reproduce

import { defineProps, withDefaults } from 'vue'

function fakeComponent<T extends string | null>() {
    return withDefaults(defineProps<{
        test: T
    }>(), {
        test: () => 'test'  as T // this is error 
    })
}

Or

<script lang="ts" generic="T extends string | null">
withDefaults(defineProps<{
    test: T
  }>(), {
    test: () => 'test' as T // this is error 
})
</script>

What is expected?

The error not to be showing, like the non generic version:

import { defineProps, withDefaults } from 'vue'

function fakeComponent() {
    return withDefaults(defineProps<{
        test: string | null
    }>(), {
        test: () => 'test' as T
    })
}

Or

<script lang="ts" generic="T extends string | null">
withDefaults(defineProps<{
    test: T
  }>(), {
    test: () => 'test'  as T
})
</script>

What is actually happening?

withDefaults is not accepting the generic type

System Info

No response

Any additional comments?

No response

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions