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

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

Closed
cawa-93 opened this issue May 17, 2022 · 0 comments · Fixed by #5939
Closed

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

cawa-93 opened this issue May 17, 2022 · 0 comments · Fixed by #5939

Comments

@cawa-93
Copy link
Contributor

cawa-93 commented May 17, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant