I have a method, that should accepts any object, as long as all its fields are strings or numbers
I made this, which works great with duck typing
static interpolateParams(
route: string,
params: {[key: string] : string | number}) : string {
const parts = route
.split("/")
.map(part => {
const match = part.match(/:([a-zA-Z09]*)\??/);
if (match) {
if (!params[match[1]]) {
console.error("route argument was not provided", route, match[1]);
return part;
}
return params[match[1]];
}
else {
return part;
}
})
return "/" + parts.slice(1).join("/");
}
and call
interpolateParams("/api/:method/:value", {method: "abc", value: 10});
Now I want to make interpolateParams
to accept any interface for route params
.
interpolateParams<IABCParams>("/api/:method/:value", {method: "abc", value: 10});
Problem is that it still should match constraints for all fields being strings or numbers
Is there a way to specify generic constraint on all fields of given interface to be of certain type?
I tried that
static interpolateParams<T extends {[key: string] : string | number}>(
route: string,
params: T) : string {
and obviously got this
Type 'IABCParams' does not satisfy the constraint '{ [key: string]: string | number; }'.
Index signature is missing in type 'IABCParams'.
Thanks