Currently, I am faced with the challenge of sharing type definitions between my server and front-end. These definitions are stored in a separate npm package that both installations utilize. The issue arises on the front-end where variables containing ObjectIds need to be typed as such, while on the client side, they are assumed to be primitive strings.
Multiple occurrences on the client trigger the error message:
Type 'ObjectId' is not assignable to type 'string'.
I am seeking advice on the simplest way to address this error. Is it possible to instruct Typescript to allow string assignment to ObjectId and vice versa on the client side? Should I attempt to override the Mongoose definition of ObjectId?
One approach I am considering involves an override like so:
declare global {
export interface MyInterface1 {
variableWithObjectId1: string
}
export interface MyInterface2 {
variableWithObjectId2: string
}
}
Although this method is suggested for a similar issue, I have yet to successfully implement it myself.
I am hopeful that there exists a solution to globally transform ObjectId to string upon importing the library into the client environment.