Reputation: 7172
I am writing a Request helper where I would like to define custom properties within an object rather than set them as parameters.
Therefore I would like the code below to work
import { IRequest } from './request'
export default class Request implements IRequest {
constructor({baseUrl: string, timeout: string}: object) {}
}
Interface:
export interface IRequest {
new: ({ baseUrl: string, timeout: number }: object): void
}
Without type object
I can see an error that indicates that a parameter in constructor should have a typedef
, which is fair - but when I assign :object
(as above) I am getting: [tslint] variable name clashes with keyword/type [variable-name]
.
Could you advice the proper way? I am probably doing something wrong with type definition. Tried {[key: string]: any}
but no luck too.
Upvotes: 0
Views: 158
Reputation: 250812
If you want them to pass an object that has the properties baseUrl
and timeout
, you need to name it first, then type it. Like this:
// name: type
constructor(obj: {baseUrl: string, timeout: string}) {}
Simplified example:
class Example {
constructor(obj: { baseUrl: string, timeout: string }) {
console.log(obj.baseUrl);
console.log(obj.timeout);
}
}
const request = new Example({ baseUrl: 'localhost', timeout: '5s' });
Upvotes: 1