Feature(s) impacted
Development with TypeScript.
Observed behavior
I’m having trouble with the following type:
interface MyType<
S extends TSchema,
N extends TCollectionName<S> = TCollectionName<S>,
F extends TFieldName<S, N> = TFieldName<S, N>,
> {
collectionName: N
field: F
}
I would like to be able to dynamically use this type on any collection of my schema, like so:
const someValue: MyType<Schema> = {
collectionName: 'User',
field: 'Id',
}
(Schema is from typings.ts
)
I am getting the following error on field
:
TS2322: Type string is not assignable to type never
index. ts: The expected type comes from property field which is declared here on typeMyType<Schema, keyof Schema, never>
That never
at the end of my generics doesn’t look good…
If I attempt to specify N
:
const someOtherValue: MyType<Schema, 'User'> = {
collectionName: 'User',
field: 'Id',
}
TypeScript doesn’t scream anymore, but I don’t see any relations fields! Only plain fields:
const someOtherValue: MyType<Schema, 'User'> = {
collectionName: 'User',
field: 'Organization:Id',
}
TS2322: Type ‘Organization:Id’ is not assignable to type TFieldName<Schema, ‘User’>
index. ts: The expected type comes from property field which is declared here on type
MyType<Schema, 'User', TFieldName<Schema, 'User'>>
So… how can I properly dynamically type these objects?
Expected behavior
My API can use the typings from typings.ts
in a dynamic manner, allowing me to absolutely mess around with my schema with minimal boilerplate.
I’m trying to use this in the context of an in-house notification plugin. The plugin works magic, but I once had a notification crash because I messed up my projection ='D… so I’m trying to type it now.
In the end I’d like to be able to use the projections to type the callback parameters, so I don’t attempt to use a field I didn’t project.
Context
- Agent technology: Node
- Agent (forest package) name & version:
@forestadmin/agent@1.55.1
- Database type: MSSQL