How to pass optional function parameters while skipping some of them in TypeScript?
Hi! I'm new to TypeScript and encountered a problem when working with functions that have multiple optional parameters.
Let's say I have a function that configures a database connection:
```typescript
function createConnection(
host: string,
port?: number,
username?: string,
password?: string,
database?: string
): Connection {
// ... implementation
}
```
I want to call this function by passing only `host`, `password` and `database`, skipping `port` and `username`. In JavaScript I could do something like:
```typescript
createConnection('localhost', undefined, undefined, 'secret123', 'mydb');
```
But this looks terrible — I have to pass `undefined` for every skipped parameter. It's unreadable and easy to make a mistake, especially wh
No answers yet.
Log in to leave an answer
Blogs
How to Write a REST API in Go in 30 Minutes: A Step-by-Step Guide