Description
Hi!
I'm making a reactive version of Pixi.JS library.
I've got typescript to check for 'invalid' hierarchy - e.g. Graphics can be 'parent' of Circle, while the opposite way would be a type error.
But I'd like to add custom error messages that would be emitted by TS compiler for better library user-experience
(e.g. 'Circle component cannot be a valid parent of Graphics' alongside 'Circle does not implement IParentablyBy<Graphics>' ), but couldn't find any typescript articles on this.
EDIT:
So I'm looking for someone else to propose some typescript syntax that would provide some ways to provide custom type-level error messages, which would definitely be better than the sample one below:
export function createComponent<
BlueprintClass extends BaseBlueprint,
PropsType extends BasePropsType
>(
blueprintClass: {
new(): BlueprintClass & IParentableBy<BaseBlueprint, BasePropsType>
}
) {
type ParentableByBlueprint = IParentableBy<
BlueprintClass as ChildBlueprintClass,
BasePropsType
> withCustomTypeErrorMessage( // THIS would be the magic sauce I'm hoping for
ts.typeErrors.invalidGenerics(childBlueprintClass),
`${ChildBlueprintClass} cannot be a child of ${BlueprintClass}`
);
// above isn't even a valid ts syntax - I'm making up above, but you get the idea (hopefully)
return function _componentMetaData(
props: PropsType,
children: Array<
RenderableType<
BasePropsType,
BaseBlueprint & ParentableByBlueprint
>
>
): RenderableType<PropsType, BlueprintClass> {
return {
blueprint: blueprintClass,
props,
children,
};
}
}
ps: Is there already a way to 'customize' typescript error messages? (or at least provide extra type-error messages?)
pps: I'd love to have them in-code (so typescript-compiler would actually type-check custom-error messages too), with options to get further error-info ('digging-into') to actually get to the raw error messages by TS compiler.