-
Notifications
You must be signed in to change notification settings - Fork 13k
Closed
Labels
DuplicateAn existing issue was already createdAn existing issue was already created
Description
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
cdpark0530
Metadata
Metadata
Assignees
Labels
DuplicateAn existing issue was already createdAn existing issue was already created