jsverse / transloco

🚀 😍 The internationalization (i18n) library for Angular
https://jsverse.github.io/transloco/
MIT License
1.97k stars 191 forks source link

Feature(transloco|transloco-locale): Provide complete type definitions for pipes used in strict mode #755

Open Kaemmelot opened 3 months ago

Kaemmelot commented 3 months ago

Is there an existing issue for this?

Which Transloco package(s) will this feature affect?

Transloco, Locale

Is your feature request related to a problem? Please describe

Only the transloco pipe allows to pass undefined or null as input values in strict mode. Other pipes like the date pipe or decimal pipe cannot be used with these values. Additionally, the transloco pipe does not provide the types returned by the TranslocoMissingHandler, so returning undefined there is also a problem in strict mode.

Describe the solution you'd like

I think each pipe should have multiple overloads that consider null and undefined:

transform(value: TYPE): string;
transform(value: TYPE | null | undefined): string | null | undefined;
transform(value: null | undefined): null | undefined;

The transloco pipe should probably provide similar overloads. But I'm not sure about the return value for that pipe, because the TranslocoMissingHandler can return any, which probably needs to be restricted before providing better return types for that pipe. That would be a breaking change.

I would also return the unchanged input value if it was null or undefined. This would not print anything if used in a template, but would allow to use the correct typescript features to check for these input values based on the result and without introducing a variable (for example if the input is the output of another pipe):

// pipe returning empty string:
{{ (value | somePipe | translocoDecimal) || 'N/A' }}
// pipe returning null or undefined:
{{ (value | somePipe | translocoDecimal) ?? 'N/A' }}

Describe alternatives you've considered

I have looked at how the Angular team provided their pipes, like for example the date pipe or the decimal pipe which is one reason for this ticket.

I also saw that they only provide null as return value, because they could not add undefined later on as described here, so I don't think transloco should follow that example for the return values.

Additional context

Related to #311 and #488

I would like to make a pull request for this feature

Yes 🚀

shaharkazaz commented 1 month ago

You are welcome to open a PR for it 👍