If you want to model an object that can accept anything as value, you're tempted to use one of the following approach:
const myObject: { [props: string]: any } = { foo: 'bar' }
or
const myObject: Record<string, any> = { foo: 'bar' }
The problem being that both { [props: string]: any }
and Record<string, any>
accept functions and arrays, which I believe in 99% of cases is not what you were trying to model with your type. (Playground showcase here)
We could construct a safer type based on the existing primitives of type-fest
:
import { Primitive } from 'type-fest'
/**
* Represents a POJO. Prevents from allowing arrays and functions
*/
export type PlainObject = {
[x: string]: Primitive | object
}
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too