I want to do a compile-time assertion on the following code:
interface FormFields {
[K: string]: string | boolean | number;
}
function FormTextInput<
FieldName extends keyof Fields,
Fields extends FormFields
>(fieldName: FieldName) { ... }
// should throw compile-time error:
FormTextInput<'someNumberField', { someNumberField: number }>('someNumberField')
// should NOT throw error:
FormTextInput<'someStringField', { someStringField: string }>('someStringField')
so that FormTextInput always throws an error if FieldName refers to a non-string value of Fields
Is it possible do to this at compile-time with Typescript? I have seen some docs on asserts ( https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions ) but it doesn't seem it was meant to be used for this scenario