#How to get keys of values of an interface

1 messages · Page 1 of 1 (latest)

calm eagle
#

Why the type of name is string | number | symbol and how can i fix it

delicate gust
#

!screenshot

young valveBOT
#
`!screenshot`:

Rather than screenshots, please provide either code formatted as:

```ts
// code here
```
Or even better, as an example on the TypeScript playground that is as simple as possible and reproduces the issue. This makes it easier to help you and increases the chances of getting an answer.

young valveBOT
#
Juhan280#0965

Preview:```ts
// types/Database.ts
export interface MainDB {
guild: { id: string; prefix: string };
}

export interface TestDB {
[key: string]: object;
}

export default interface Databases {
main: MainDB;
test: TestDB;
}

// classes/Database.ts
import { Document, MongoClient, ServerApiVersion } from "mongodb";
...```

#
Nctdt#2630

Preview:```ts
...
on<
DBName extends keyof Databases,
CName extends keyof Databases[DBName] & string

(dbName: DBName, name: CName) {
return this.client.db(dbName).collection(name)
}
...```

fierce flame
#

like the above snippet?

calm eagle
young valveBOT
#
Juhan280#0965

Preview:```ts
export interface MainDB {
guild: {id: string; prefix: string}
}

export interface TestDB {
[key: string]: object
}

export default interface Databases {
main: MainDB
test: TestDB
}

import {
Document,
MongoClient,
ServerApiVersion,
} from "mongodb"
...```

fierce flame
young valveBOT
#
Nctdt#2630

Preview:```ts
import {
Document,
MongoClient,
ServerApiVersion,
} from "mongodb"
export type MainDB = {
guild: {id: string; prefix: string} & Document
[k: string]: Document
}

export type TestDB = Record<string, Document>

type Databases = {
main: Mai
...```

calm eagle
#

It should only have the collection i defined

fierce flame
#

only main db?

calm eagle
fierce flame
#

okay

young valveBOT
#
Nctdt#2630

Preview:ts ... return this.client .db(dbName) .collection(name) as Databases[DBName][CName] ...

fierce flame
young valveBOT
#
Juhan280#0965

Preview:```ts
...
public getCollection<
DBName extends keyof Databases,
CName extends keyof Databases[DBName] & string

(dbName: DBName, name: CName) {
return this.client
.db(dbName)
.collection(name) as Collection<
Databases[DBName][CName]

}
}

new Database("").getCollection("main", "guild")```

calm eagle
#

Cuz later findOne would use that type from that

fierce flame
#

oh, I didn't see the collection

calm eagle
#

btw, something like that not possible?

fierce flame
#

hmm... because that's conditional type with extends keyword is not in the interface context

fierce flame
#

like my example

#

use &

calm eagle
#

Ohh

calm eagle
fierce flame
calm eagle
#

The name is intersection, but it doesn't behave like an intersection operator in math

#

But anyways

#

!solved

#

!resolved

fierce flame
#

u can use name as string