'Type 'string' cannot be used to index type 'T'

I am wondering what the correct way is to type this? I have tried quite a few ideas and can't make it happy.

I have tried <T extends object> (same error)

I also tried setting field: keyof T (which gives the error "Type '{ invalid: boolean; valid: boolean; feedback: null; }' is not assignable to type 'T[keyof T]"

writable is a svelte store.


export function createValidator<T extends Record<string, object>>(formState: T) {
  const vState = {...formState};

  const { subscribe, set, update } = writable<T>(vState);

  async function validate(field: string): Promise<boolean> {
    try {
      await validateOrReject(formState);
        
      if(field) {
        update(state => {
          state[field] = { //Type 'string' cannot be used to index type 'T
            invalid: false,
            valid: true,
            feedback: null
          };

          return state;
        });
      }

.........


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source