My linter is saying I shouldn't use object as a type in TS. However, a library that I'm using asks for generic types that extend object.
I'm therefore writing a generic component with the library that looks like this:
const Component = <T extends object>(props: CustomProps<T>): React Element => etc.
Since the linter is complaining, I've tried switching it to
const Component = <T extends Record<string, unknown>>(props: CustomProps<T>): React Element => etc.
However, if I then do something like this:
interface MyInterface {
something: string;
goes: number;
here: string;
}
const CallerComponent = () => Component<MyInterface> />
TS says that MyInterface does not satisfy the constraint Record<string, unknown>. Is there any way around this other than to do
interface MyInterface extends Record<string, unknown>
I'd rather not have to go through the entire codebase adding this to every interface so it would be great to know if there's a way I can type it where I say that T will definitely be an object.
Thanks in advance for any help.
Record<string, unknown>
will require an index signature. Why didn'tobject
work for you ? You could also tryT extends Record<keyof T, unknown>
.eslint
rules with a grain of salt. They are there to prevent people jumping the gun, but in this caseobject
might be fine{}
also gives an error.