Suppose I have an interface that looks like:
type RequestProps = "a" | "b" | "c"
interface Request {
props: RequestProps[];
}
and some code that takes the request and returns a response object:
declare function sendRequest(r: Request);
I would like to narrow r.props so that I can return a response with keys corresponding to the requested props. That is, if the request is made by a user like so:
const request = {
props: ["a", "c"]
}
let response = sendRequest(request);
That response would autocomplete response.a and response.c as string values. Something along the lines of
type Response<T extends RequestProps[]> = { [key in T[number]]: string; }
I don't want the request maker to have to indicate string literals ("a" as "a"), or a tuple (["a", "c"] as ["a", "c"]), I would like sendRequest to be able to infer it or hint contextually or guard in some way that allows the type to be narrowed down to the actual literal values that the array contains, no matter how verbose (I will narrow down every permutation if I have to). Is this possible? String enums seem to have the same issue as string literals. Thanks!