How to set a default value for a function parameter object in TypeScript?
Hello! I just started learning TypeScript, and I have a problem with function parameters that are objects.\n\nLet's say I have a function that takes a settings object:\n\n```typescript\nfunction createUser(config: { name: string; age: number; isAdmin: boolean }) {\n // ...\n}\n```\n\nI want the `config` parameter to be optional, and if it's not passed, default values should be used. In JavaScript I would do it like this:\n\n```javascript\nfunction createUser(config = { name: 'Guest', age: 18, isAdmin: false }) {\n // ...\n}\n```\n\nBut in TypeScript this doesn't work because the type of `config` must be defined. I tried different options:\n\n1. Just specify a default value — TypeScript complains about type mismatch.\n2. Use `Partial
No answers yet.
Log in to leave an answer