smapiot / piral

🚀 Framework for next generation web apps using micro frontends. ⭐️ Star to support our work!
https://piral.io
MIT License
1.68k stars 125 forks source link

InjectionToken<...> for public providers in piral-ng #703

Closed hwanders closed 2 months ago

hwanders commented 2 months ago

New Feature Proposal

Add typed InjectionToken<...> constants to piral-ng and use them when providing piral, Props and Context using those tokens in the dependency injection container.

Description

For downward-compatibility the string-based providers should probably be kept. The relevant source code parts are:

Background

Some application developers prefer the new inject(...) funtion over the classic @Inject(...) annotation. inject is typed more strictly, it does not allow a simple string parameter anymore, like @Inject. So writing inject('piral') will fail in a type checking error.

Since the token passed to inject must exactly match the one used as provide key, developers currently can not use the the inject function without using type-avoiding hacks (like inject('piral' as any)).

Example

The following is an ad-hoc change, without even compiling/building Piral. https://github.com/hwanders/piral/commit/84347a912fda06914a6d4048d018f567b281086b

FlorianRappl commented 2 months ago

Sounds great - want to make a PR?