I’m having trouble enforcing the same generic type for two instances of TValidationAdapter
. Without an explicit field (_typeEnforcer
) using the generic type, TypeScript isn’t complaining when the types differ (see video).
export interface TValidationAdapter<
GValue,
GValidateContext extends TBaseValidateContext<GValue> = TBaseValidateContext<GValue>
> {
_validationChain: TValidationChain<GValue, GValidateContext>;
_typeEnforcer?: GValue; // TODO: This field ensures that TypeScript enforces the generic type GValue consistently across different instances of TValidationAdapter
validate: <GInnerValidateContext extends GValidateContext = GValidateContext>(
cx: GInnerValidateContext
) => Promise<GInnerValidateContext>;
append: (validator: TValidationAdapter<GValue>) => TValidationAdapter<GValue>;
clone: () => TValidationAdapter<GValue>;
push: (...validateFunctions: TValidationLink<GValue>[]) => void;
}
How can I enforce the same generic type without needing the _typeEnforcer
field?
- Source for Context: https://github.com/inbeta-group/monorepo/blob/42-openapi-router/packages/validation-adapter/src/types.ts
- Video: https://youtu.be/k6yKNsTKNe4
Thanks 🙂