1

So you can declare a generic index signature for example like this:

type GenericType<A> = { firstField: A, secondField: any };

type GenericIndex<T extends object> = {
  [K in keyof T]: GenericType<T[K]>;
};

But is it possible to have a generic on field level?

See <R> in following example:

type GenericType<A, R> = { firstField: A, secondField: R };

type GenericIndex<T extends object> = {
  // this line is not compiling because "<R>" is no valid ts syntax here
  [K in keyof T]<R>: GenericType<T[K], R>;
};

Example usage:

type SomeType = { a: number, b: string }


const generic: GenericIndex<SomeType> = {
  a: { firstField: 1, secondField: "anyString"},
  b: { firstField: "someString", secondField: 2}
}

generic.a.secondField // type should be string but is any
generic.b.secondField // type should be numer but is any

Please don't take the example to serious. It could also be something more complex.

ysfaran
  • 5,189
  • 3
  • 21
  • 51
  • The syntax you tried is not valid, but I am unsure what the intent is. Could you provide an example as to how this generic type parameter would work ? Also `GenericFunction` is not a generic function, it is a generic type that happens to be a function, this would be a generic function: `type GenericFunction = (args: A) => any;` The difference being who gets to decide what `A` is, the declaration or the call site. So you might be looking for `type GenericIndex = { [K in keyof T]: (a: T[K]) => R; };` but I am unsure without a usage example – Titian Cernicova-Dragomir Nov 03 '19 at 19:25
  • @TitianCernicova-Dragomir I edit my question. Hope it clears things up. – ysfaran Nov 03 '19 at 20:41
  • 1
    Sounds like you want a combination of generic type specification and type inference, for which [partial type parameter inference](https://github.com/Microsoft/TypeScript/issues/10571) would be desirable. Unfortunately there is no such partial generic inference in TypeScript, so you have to [work around that](https://stackoverflow.com/a/55754981/2887218). – jcalz Nov 03 '19 at 21:14
  • Like [this](https://www.typescriptlang.org/play/#code/C4TwDgpgBA4gKgHgIIBooCUB8UC8UDeUAZgIwBcUqxATBelAL4DcAsAFCiSwCSCc2efOyhQA2gGkoASwB2UANYQQAeyJQ4AXQrw+EjWgCuM+TOUB3GZlZtm7TtADKygLYQ44aIKgBDCjIPOAEYQAE5ogRQAzsAhsgDmjOwy3q6RYN4AxtAAwgYhISAEwlAZyjLRPpEw3LhQfJgAFACUuNgIMFAQAB7AEDIAJpE89Y1x2i042HHWIqXlwFBxfbXeVbxOru6QjU0NQmwiIr4ExORQJGhEtFAARN43jCjFIhEnpBQ3kTeX19SJB4wmsUljIAHTeUFXKAAemhUGisRkcWBfVBgUhf1hUH8QVC7AYSRSEDSmWgABEAs5CvtZmUKqtqrU+GgOt1egMhtURg1+pSQHAKHA0GNYBMpjMSnSFiCVmsGv4ADYKgCElSgGzcHjQNMOx0I73OPw+90ezygr31Z0+3xoFD+BIBDCBAJB4IxMLhCPiKLB6KhWJxwRC+KAA) maybe? – jcalz Nov 03 '19 at 21:17
  • @jcalz thanks! I actually know about this workarrounds.. are there actual plans to add this feature? The issue is 3 years old and the proposal 1 year.. everytime when I have issues with typings it leads me back to this missing feature.. it's so frustrating because IMO the workarround are uglifying the code expecially if you want to implement a open source library .. on top of that it's kinda weird to have these curried functions or the dummies because in the transpiled javascript they are not necessary at all.. – ysfaran Nov 03 '19 at 21:48

0 Answers0