我正在为 nodejs 使用“mongodb”驱动程序。我正在尝试将 ttl 的可选参数添加到此现有方法中:
public async createIndex(index: any, unique: boolean): Promise<void> {
await this.collection.createIndex(index, { unique });
}
有没有比这更优雅的方法:
public async createIndex(index: any, unique: boolean, ttl: number): Promise<void> {
await ttl ? this.collection.createIndex(index, { unique, ttl }) : this.collection.createIndex(index, { unique });
}
我读到将 null 传递给可选参数会使其行为怪异。