0

I have the following interfaces:

interface ColDef<Entity, Field extends keyof Entity> {
    field: Field;
    valueGetter(value: Entity[Field], entity: Entity): any
}


interface Options<Entity> {
    colDefs: ColDef<Entity, ??>[]
}

const options: Options<{ name: string }> = {
    colDefs: [{
        field: 'name',
        valueGetter(value) {
        // how do I make value typed as string??
        }
    }]
}

I want to infer the entity field type in the valueGetter function. The issue is that I ColDef is required the key as the second generic, but I'm passing an array. How can I solve this?

1 Answer 1

1
interface ColDef<Entity, Field extends keyof Entity> {
  field: Field;
  valueGetter(value: Entity[Field], entity: Entity): any
}

type A = {
  name: string;
  age: number;
}


interface Options<Entity> {
  colDefs: { [K in keyof Entity]: ColDef<Entity, K> }[keyof Entity][]
}

type What = { [K in keyof A]: ColDef<A, K> }
//   ^?
// type What = {
//     name: ColDef<A, "name">;
//     age: ColDef<A, "age">;
// }

type What1 = { [K in keyof A]: ColDef<A, K> }[keyof A]
//   ^?
// type What1 = ColDef<A, "name"> | ColDef<A, "age">


const options: Options<A> = {
  colDefs: [{
    field: 'name',
    valueGetter(value) {
      //        ^? (parameter) value: string
      // how do I make value typed as string??
    }
  }]
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.