A generic type variable ErrorId constrained by the generic type variable Id of the CommonErrors object indicates the type picked from the Id and its exact type is useful in picking the specific error from the storage.
// Example usage.
import { CommonErrors } from '@angular-package/error';
// Define new `TestClass` to add error to the map storage.
export class TestClass<Id extends string> extends CommonErrors<Id> {
public get errors(): Map<Id, any> {
return super.errors;
}
public set<ErrorId extends Id>(
problem: string,
fix: string,
id: ErrorId
): this {
// <--- Use the isAllowedId() here ----------------
if (super.isAllowedId(id)) {
this.errors.set(id, new Error(problem, fix, id));
}
// <-----------------------------------------------
return this;
}
}
// Initialize the `TestClass`.
const generalErrors = new TestClass('EG: 4332', 'EG: 4331', 'EG: 4330');
// Set the errors.
generalErrors
.set(
'Bad parameter type, detected number',
'Provide proper type, the `string`',
'EG: 3000' as any // <--- Wrong ID
)
.set(
'Detected numbers',
'Provide only letters',
'EG: 4335' as any // <--- Wrong ID
);
// Returns Map(0) {size: 0}
generalErrors.errors;
// Returns `false`.
generalErrors.has('EG: 3000' as any);
// Returns `false`.
generalErrors.has('EG: 4335' as any);