Skip to content

Incorrectly sets type to "never" #32375

@TidyIQ

Description

@TidyIQ

TypeScript Version: 3.5.3

Search Terms: type is not assignable to type 'never', keyof, generic

Code

interface FormState {
  [key: string]: {
    fields: {
      [key: string]: {
        isInvalid: boolean;
        isValid: boolean;
        labelWidth: number;
        showPassword?: boolean;
        value: string;
      };
    };
    open?: boolean;
  };
}

let formState: FormState = {
  foo: {
    fields: {
      bar: {
        isInvalid: false,
        isValid: false,
        labelWidth: 0,
        value: ""
      }
    }
  }
};

interface SetFieldValueProps<
  T extends keyof FormState = keyof FormState,
  F extends keyof FormState[T]["fields"] = keyof FormState[T]["fields"],
  K extends keyof FormState[T]["fields"][F] = keyof FormState[T]["fields"][F]
> {
  form: T;
  field: F;
  key: K;
  value: FormState[T]["fields"][F][K];
}

const doSomething = (props: SetFieldValueProps): void => {
      const { form, field, key, value } = props;
      formState[form].fields[field][key] = value;
}

Expected behavior:

No issue. Type formState[form].fields[field][key] correctly gets the type as const key: "isInvalid" | "isValid" | "labelWidth" | "showPassword" | "value" and value correctly gets the type as const value: string | number | boolean | undefined. Nowhere in there is never defined as a possible type.

Actual behavior:

Type 'string | number | boolean | undefined' is not assignable to type 'never'.
  Type 'undefined' is not assignable to type 'never'.

Playground Link: Link to playground

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions