A small type trick implemented in the LIFF SDK's Pluggable SDK
Hello! I'm odan, and I'm in charge of developing the LIFF SDK. In this article, I'll introduce a small type trick implemented in the LIFF SDK's Pluggable SDK.
What is the Pluggable SDK?
The standard LIFF SDK is imported as import liff from "@line/liff".
In this case, the liff object includes methods such as liff.requestFriendship and objects such as liff.permission. These methods and objects aren't necessarily required by every LIFF app. Objects that aren't used cause an unnecessary increase in bundle size.
The Pluggable SDK solves this problem. The Pluggable SDK provides a liff object (hereafter, the LIFF Core object) that has only the minimum required methods.
Here's an example of how to use it:
Example of using a method not included in the minimum required set:
By using the Pluggable SDK, you can reduce the bundle size of your LIFF app.
Extending the type of the LIFF Core object
As mentioned earlier, in the world of JavaScript, calling the liff.requestFriendship method on the LIFF Core object causes a runtime error.
So that developers can notice this problem early, only the minimum required methods are defined on the LIFF Core object in the world of types as well.
To extend the minimum required types, you need to import the package for each module.
Extending types with Declaration Merging
So how is this mechanism—where simply importing a package resolves the type error—achieved?
TypeScript has a technique called Declaration Merging, which is what's used here. To put it simply, the members of interfaces defined with the same name are merged and can be treated as one.
The documentation above introduces the following sample code:
This technique is used to extend the type of the LIFF Core object. Let's take a look at the actual contents of @line/liff/request-friendship.
The interface declaration starting on line 5 is the key point. Writing the declaration of LiffCore causes the type information to be merged. This lets you immediately notice careless mistakes, such as forgetting to add the object, when using the LIFF Core object.
Incidentally, Vue.js plugins use a similar mechanism.
Wrap-up
I introduced a small type trick implemented in the Pluggable SDK.
Thanks to Declaration Merging, you can use types with confidence, so please consider the Pluggable SDK when you want to improve your bundle size.