I defined a service type
and my purpose is to create a function with two params.
- first one is service type which is key of type Service
- second one is a string array of the attribute keys per the first argument.
something like below
type MyService = {
service1: { x: string, y: number },
service2: { m: string, n: number },
};
function useService(ns: keyof MyService, serviceNames: Array<keyof MyService[typeof ns extends keyof MyService ? typeof ns : never]>): void {
console.log(serviceNames);
}
useService('service1', ['x', 'y']); // now the second parameter type is always never
I don't know whether ts could check the runtime type? Please advice.