Reputation: 1386
My object looks like this:
const searchOptions: any = {
fullText: 'hifi',
'brand.id': [1,2,3,4],
'category.id': [1,2,3,4],
'country.id': [1,2,3,4],
'property.id': [1,2,3,4],
}
and that's very handy because the API accepts the same query params.
How do I type this object in TypeScript?
Upvotes: 0
Views: 70
Reputation: 1386
I guess I asked the question wrong, I ended up doing like this:
class SearchOptions {
fullText: string
'brand.id': string[]
'category.id': string[]
'country.id': string[]
'property.id': string[]
constructor(
fullText: string,
brandId: string[],
categoryId: string[],
countryId: string[],
propertyId: string[],
) {
this.fullText = fullText
this['brand.id'] = brandId
this['category.id'] = categoryId
this['country.id'] = regionCountryCodes
this['property.id'] = propertyId
}
}
Upvotes: 0
Reputation: 33091
For typescript 4.1 or later, you can use Key Remapping
type Props = 'brand' | 'category' | 'country' | 'property'
type Base = {
fullText: string
}
type Obj = {
[Prop in Props as `${Prop}.id`]: number[]
} & Base
const searchOptions: Obj = {
fullText: 'hifi',
'brand.id': [1, 2, 3, 4],
'category.id': [1, 2, 3, 4],
'country.id': [1, 2, 3, 4],
'property.id': [1, 2, 3, 4],
}
If these props 'brand' | 'category' | 'country' | 'property'
are keys of some object, you can use keyof SomeObj
to retrieve them
Upvotes: 5