I have a custom input component that has an onChange
method.
type CustomInput = {
dataType: string;
onChange: (data: Record<string, unknown) => unknown; // this is causing issues, might need generic type
}
Usage:
type MyProp = {
name: string;
date: Date;
}
<CustomInput dataType="myPropData" onChange={(prop: MyProp) => { /* do stuff with prop */ }} />
Inside of the component, we perform some logic then call onChange(data)
. data
will be different based on what dataType
is passed.
Another usage may be
type MyOtherProp = {
name: string;
title: string;
count: number;
}
<CustomInput dataType="myOtherPropData" onChange={(prop: MyOtherProp) => { /* do stuff with other prop */ }} />
Issue:
Because MyProp
and MyOtherProp
are not really Record<string, unknown>
, I’m getting errors.
It’s clear why there are errors and I’m sure the answer is “use generics”, but it’s unclear to me how I can set the proper type on the onChange
method. Using unknown
params also throws errors and I’d like to avoid using any
.