0

I am using arg and fp-ts. I want to have a function that returns type based on one of the inputs key value.

export const getArg =  <T extends Spec> (args: Result<T>) => (argName: {[K in keyof T]: T[K]}): O.Option<T[K]> =>
    O.fromNullable(args[argName])

But it says that I can't index with K in keyof T, despite K being the actual keys. And it won't see K in Option. I need some help with types.

Result

    export type Result<T extends Spec> = { _: string[] } & {
        [K in keyof T]?: T[K] extends Handler
            ? ReturnType<T[K]>
            : T[K] extends [Handler]
            ? Array<ReturnType<T[K][0]>>
            : never;
    };
BZKN
  • 1,499
  • 2
  • 10
  • 25
Danil
  • 103
  • 7

1 Answers1

1

I couldn't make it work with arg library. But I was able to extract type with command-line-args

export const getArg =
  (args: CommandLineOptions) =>
  <K extends keyof CommandLineOptions>(
    argName: K
  ): O.Option<CommandLineOptions[K]> =>
    O.fromNullable(args[argName])

The right way to it was to use extends keyof instead of in keyof, and then reference the desired type via F[K]

Danil
  • 103
  • 7