Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support strictTemplates / All pipes should accept null and undefined #347

Closed
MickL opened this issue Feb 9, 2021 · 4 comments
Closed

Support strictTemplates / All pipes should accept null and undefined #347

MickL opened this issue Feb 9, 2021 · 4 comments

Comments

@MickL
Copy link

MickL commented Feb 9, 2021

With Angular strict mode all inputs need to be optional:

export class myComponent implements OnInit {
  @Input() myDate?: number;
}

Or using the async pipe returns null:

<app-my-compontent [myDate]="myDate$ | async"></app-my-component>

But then the compiler is throwing an error with ngx-date-fns pipes, e.g.:

Type 'undefined' is not assignable to type 'DateFnsInputDate'.
{{ myDate | dfnsFormat: 'dd.MM.yyyy'}}

I would suggest to add strict typechecking to tsconfig:

"angularCompilerOptions": {
    "strictInjectionParameters": true,
    "strictInputAccessModifiers": true,
    "strictTemplates": true,
    "preserveSymlinks": true
  }

and add a questionmark to all pipe inputs to support undefined and also accept null, e.g. the format pipe:

transform(
    date?: DateFnsInputDate | null,
    ...
  ): string {
    if (!date || isInvalidDate(date)) {
      return '';
    }
    ...
  }
}
@joanllenas
Copy link
Owner

Hi @MickL ,
I'll take a look at this during the weekend.
Thanks!

@MickL MickL changed the title Alle pipes should accept undefined Alle pipes should accept null and undefined Feb 10, 2021
@MickL
Copy link
Author

MickL commented Feb 10, 2021

I added "null" to the example because when using the async pipe its return type is T | null: {{ (myDate$ | async) | dfnsFormat: 'dd.MM.yyyy'}}

@MickL MickL changed the title Alle pipes should accept null and undefined Support strictTemplates / All pipes should accept null and undefined Feb 15, 2021
joanllenas added a commit that referenced this issue Feb 21, 2021
The format* pipes family (the ones that return string) now accept null and undefined so they can be
piped with the async pipe on strict mode

#347
@joanllenas
Copy link
Owner

@MickL v7.0.2 implements this change.

Note that this has only been implemented for pipes that return a string representation of a date. The rest of the pipes that make calculations with dates are not affected because the input has to be valid.

@MickL
Copy link
Author

MickL commented Feb 21, 2021

Awesome :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants