fix(TS): extending canvas and object event types #8926
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
I'll progressively update the PR to make fabric (beta5 currently) more type-friendly for the consumer.
Changes
type
as interfaces allow "Interface merging" and module augmentation: https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentationEventTypeDefs
so that they can be used along with module augmentation. It looks like that if you do module augmentation, e.g.declare module "fabric"
as shown below, you can't anymore import any type from a submodule (e.g.import { ModifiedEvent } from "fabric/dist/src/EventTypeDefs"
).This is very inconvenient when you need to type the event handler for instance:
onModified(event: ModifiedEvent) {}
.So far I haven't found any solution for this other than re-exporting the types so that they are directly available using
import { ModifiedEvent } from "fabric/"
In Action
I can now do the following module augmentation so that
object.fire("custom:transform:begin")
doesn't raise an error: