Skip to content

withDefaults() has type error when used default factory for type #5938

@cawa-93

Description

@cawa-93

Link to minimal reproduction

https://www.typescriptlang.org/play?#code/CYUwxgNghgTiAEAXAngBwQOQPaIKoDtQAzAS3xGAB4AVAPngF55r4QAPREQgZ3gFdCIUuWDwA-PHIA3EDHgAuZgG4AsACh160JFgIU6eAEl8RWQBEhUPhESUACgBpm9Ji3acek6xHgAfLwC2AEayfvDciDBkAOZhQVhYECBQ+GHcyMGJYQBiAmCIJFipEiyKABSoMFio3Ip2AJSM9NSqGmra0HBIaAjGpjAWRFY23DQu8ADe6vAz8ADaANLwZPAA1iDIWETMALpiin3mlta21E7YeILCFDSLO7S0rQC+rVrgnXo98HZVNQDqJEQAAtBsNEKMAEJQbggJygk7ccZQmHwABkk2ms0Wy1S60223hIx2iiW7i4wF4eK28GRCAkFwIxDIN1pd3oimksmer3a7108CIeQKRXgAHdASDjiN7L9uHCpeDWBxybxDgMFaMftVEbQKrK6rKnMQwbV4ITwfUDdqAcDzZrDWaNY9NGp2KgsDBEPAOvzBfh8oVUkzyFqajQeqHEWVLfAAErJYBFCDIcPoSPOtougD0WfgAGEsAF3eR8IgAHRSPggF1kTgwIZgBCRjFqGYkYD7cKRGLqJ6Y9Ti20asrBkCRmXa3X1JxTVvLYDlRoMegAWSgwLLMBSiYC0bLiCwAGVu-homUAMwANnqZe4EBIjbKACZ6r36kA

Steps to reproduce

Create component with

interface Props {
  id?: string
}
  
withDefaults(defineProps<Props>(), {
  id: () => Math.random().toString(36).slice(2)
})

What is expected?

Has no error

What is actually happening?

TS Show error

interface Props {
  id?: string
}
  
withDefaults(defineProps<Props>(), {
  id: () => Math.random().toString(36).slice(2) // Type '() => string' is not assignable to type 'string'
})

System Info

System:
    OS: Windows 10 10.0.19044
    CPU: (16) x64 AMD Ryzen 7 4800H with Radeon Graphics
    Memory: 3.15 GB / 15.42 GB
  Binaries:
    Node: 16.14.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.10 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 8.3.1 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.19041.1266.0), Chromium (101.0.1210.47)
    Internet Explorer: 11.0.19041.1566

Any additional comments?

In my script, I want to create a props that accepts a string or generates a random one. To generate a random string for each component, I need to use a factory for a primitive type. It works great in runtime but I encounter a type error

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions