Other than that it's just the "cost of doing business". Those interfaces had no idea that you would be adding your plugin. So now you are merging your plugin into those existing interfaces.
type space: where TypeScript compile time types live
value space: where JavaScript runtime values live
The makeAPI function is pure JavaScript and lives in value space. With the help typeof and ReturnType we pulled API into type space for compile time type checking. Monkey patching those interfaces happens at runtime in value space, so TypeScript can't really track where it is going to end up - yet value space code is going to try to access it in those places so it becomes necessary to tell TypeScript where it is going to show up.
TypeScript is a compile time static type checker. Here you are assembling an object dynamically at runtime (in value space). TypeScript hates that - so you have to take TypeScript by the hand and explain to it like it's five.
The big help here is Partial<URLBuilder> because it makes all the members of the object optional so things can be added piece by piece. However for methods we have to assert that this will be a full blown URLBuilder by the time the method runs.
In the end you as the developer have to take the responsibility of asserting that you are sure that builder is no longer Partial but a fullblown URLBuilder.
Trouble maker and Problem solver ⚙️🔧
Loves simplicity, hates bullshit 💩.
Productivity obsessed, avid learner 🖥🚀
Sport and outdoor freak 🧗⛰
Metalhead 🎸🤘 Father of 2 👨👩👦👦
Opinions are my own
Types you don't care about are unknown
Use typeof type operator and ReturnType to generate the type for you.
Other than that it's just the "cost of doing business". Those interfaces had no idea that you would be adding your plugin. So now you are merging your plugin into those existing interfaces.
Also it's not duplication. There is type declaration space and variable declaration space or as I like to put it:
The
makeAPI
function is pure JavaScript and lives in value space. With the helptypeof
andReturnType
we pulledAPI
into type space for compile time type checking. Monkey patching those interfaces happens at runtime in value space, so TypeScript can't really track where it is going to end up - yet value space code is going to try to access it in those places so it becomes necessary to tell TypeScript where it is going to show up.TypeScript is a compile time static type checker. Here you are assembling an object dynamically at runtime (in value space). TypeScript hates that - so you have to take TypeScript by the hand and explain to it like it's five.
The big help here is
Partial<URLBuilder>
because it makes all the members of the object optional so things can be added piece by piece. However for methods we have to assert thatthis
will be a full blownURLBuilder
by the time the method runs.In the end you as the developer have to take the responsibility of asserting that you are sure that
builder
is no longerPartial
but a fullblownURLBuilder
.Wow, wow, wow. Lots of useful advanced stuff here 🤩
Super useful comment.
Love this reply.
Top notch.👏👏👏