Simple trick to optimize your Typescript code

Marcellin Nshimiyimana
2 min readJul 26, 2021

Import type in older versions of Typescript

Audience

Anyone who works on a project that uses outdated Typescript.

If you write Typescript on the backend and frontend like me, you know that sometimes your backend code can end up in the frontend bundle.

The Typescript team came with ingenious idea to address this particular issue.

This feature is only available if you are using Typescript v3.8.3, so the question is: What do you do if you are stuck on older version?

  1. You could upgrade, but I am sure you are stuck at an old version for a good reason
  2. You could also implement the feature yourself!

This trick comes in handy when you have emitDecoratorMetadata set to true.

In example below, we want to preventPayment class from being imported in our JavaScript output.

Typescript v3.2.4

This is how we achieve this behavior

Typescript v3.2.4

Conclusion

The type keyword gives developers ability to take advantage of all typing information available to them, without worrying about exposing the actual code to the end user.

Clap if you find this insightful and share your own Typescript tricks in the comment section!

--

--

Marcellin Nshimiyimana

Always debugging, because problem knowledge equals half the solution!