angular-cn/packages/common/src
JoostK e9ead2bc09 feat(ivy): more accurate type narrowing for `ngIf` directive (#30248)
A structural directive can specify a template guard for an input, such that
the type of that input's binding can be narrowed based on the guard's return
type. Previously, such template guards could only be methods, of which an
invocation would be inserted into the type-check block (TCB). For `NgIf`,
the template guard narrowed the type of its expression to be `NonNullable`
using the following declaration:

```typescript
export declare class NgIf {
  static ngTemplateGuard_ngIf<E>(dir: NgIf, expr: E): expr is NonNullable<E>
}
```

This works fine for usages such as `*ngIf="person"` but starts to introduce
false-positives when e.g. an explicit non-null check like
`*ngIf="person !== null"` is used, as the method invocation in the TCB
would not have the desired effect of narrowing `person` to become
non-nullable:

```typescript
if (NgIf.ngTemplateGuard_ngIf(directive, ctx.person !== null)) {
  // Usages of `ctx.person` within this block would
  // not have been narrowed to be non-nullable.
}
```

This commit introduces a new strategy for template guards to allow for the
binding expression itself to be used as template guard in the TCB. Now,
the TCB generated for `*ngIf="person !== null"` would look as follows:

```typescript
if (ctx.person !== null) {
  // This time `ctx.person` will successfully have
  // been narrowed to be non-nullable.
}
```

This strategy can be activated by declaring the template guard as a
property declaration with `'binding'` as literal return type.

See #30235 for an example where this led to a false positive.

PR Close #30248
2019-05-16 09:48:40 -07:00
..
directives feat(ivy): more accurate type narrowing for `ngIf` directive (#30248) 2019-05-16 09:48:40 -07:00
i18n Revert "feat(ivy): use i18n locale data to determine the plural form of ICU expressions (#29249)" (#29918) 2019-04-15 16:55:51 -07:00
location feat(common): provide replacement for AngularJS $location service (#30055) 2019-04-24 13:49:29 -07:00
pipes docs: update date pipe example for minutes and seconds (#30281) (#30295) 2019-05-07 10:29:31 -07:00
common.ts feat(ivy): convert [ngStyle] and [ngClass] to use ivy styling bindings (#28711) 2019-02-20 13:46:15 -08:00
common_module.ts docs: common with `@publicApi` tags (#26595) 2018-10-19 14:35:53 -07:00
cookie.ts feat(common): on-by-default XSRF support in HttpClient (#18108) 2017-07-13 17:22:02 -07:00
dom_tokens.ts docs: common with `@publicApi` tags (#26595) 2018-10-19 14:35:53 -07:00
platform_id.ts docs: convert all `@experimental` tags to `@publicApi` tags (#26595) 2018-10-19 14:35:52 -07:00
version.ts docs: common with `@publicApi` tags (#26595) 2018-10-19 14:35:53 -07:00
viewport_scroller.ts refactor(ivy): migrate ɵɵ prefix back to Δ (#30362) 2019-05-14 16:52:15 -07:00