angular-docs-cn/aio/content/guide
Alex Rickabaugh f1269d98dc feat(ivy): input type coercion for template type-checking (#33243)
Often the types of an `@Input`'s field don't fully reflect the types of
assignable values. This can happen when an input has a getter/setter pair
where the getter always returns a narrow type, and the setter coerces a
wider value down to the narrow type.

For example, you could imagine an input of the form:

```typescript
@Input() get value(): string {
  return this._value;
}

set value(v: {toString(): string}) {
  this._value = v.toString();
}
```

Here, the getter always returns a `string`, but the setter accepts any value
that can be `toString()`'d, and coerces it to a string.

Unfortunately TypeScript does not actually support this syntax, and so
Angular users are forced to type their setters as narrowly as the getters,
even though at runtime the coercion works just fine.

To support these kinds of patterns (e.g. as used by Material), this commit
adds a compiler feature called "input coercion". When a binding is made to
the 'value' input of a directive like MatInput, the compiler will look for a
static field with the name ngAcceptInputType_value. If such a field is found
the type-checking expression for the input will use the static field's type
instead of the type for the @Input field,allowing for the expression of a
type conversion between the binding expression and the value being written
to the input's field.

To solve the case above, for example, MatInput might write:

```typescript
class MatInput {
  // rest of the directive...

  static ngAcceptInputType_value: {toString(): string};
}
```

FW-1475 #resolve

PR Close #33243
2019-10-24 09:49:38 -07:00
..
accessibility.md docs: port accessibility app from stackblitz to aio (#32377) 2019-09-10 13:11:48 -04:00
ajs-quick-reference.md docs: fix spelling error in angular component (#32971) 2019-10-23 11:46:41 -07:00
angular-compiler-options.md docs: fix typo in guide (#32893) 2019-10-10 13:54:58 -07:00
animations.md feat(docs-infra): implement figure styles (#33259) 2019-10-23 12:59:34 -07:00
aot-compiler.md feat(ivy): input type coercion for template type-checking (#33243) 2019-10-24 09:49:38 -07:00
aot-metadata-errors.md docs: edit and organize aot doc (#32028) 2019-08-21 15:19:30 -07:00
app-shell.md docs: update universal docs for new ivy implementation (#33040) 2019-10-21 11:25:44 -04:00
architecture-components.md feat(docs-infra): implement figure styles (#33259) 2019-10-23 12:59:34 -07:00
architecture-modules.md feat(docs-infra): implement figure styles (#33259) 2019-10-23 12:59:34 -07:00
architecture-next-steps.md docs: fix links to setup and cli docs (#26463) 2018-10-16 14:14:19 -07:00
architecture-services.md feat(docs-infra): implement figure styles (#33259) 2019-10-23 12:59:34 -07:00
architecture.md feat(docs-infra): implement figure styles (#33259) 2019-10-23 12:59:34 -07:00
attribute-directives.md feat(docs-infra): implement figure styles (#33259) 2019-10-23 12:59:34 -07:00
bazel.md fix(bazel): do not modify tsconfig.json (#30877) 2019-06-11 14:23:00 -07:00
bootstrapping.md refactor(docs-infra): remove `linenums=false` since it is now the default (#31674) 2019-07-24 14:38:54 -07:00
browser-support.md fix(common): remove deprecated support for intl API (#29250) 2019-10-17 20:44:17 -04:00
build.md docs: clarify instructions on where to create the proxy.conf.json file (#33244) 2019-10-18 14:40:57 -04:00
cheatsheet.md docs: clarify meaning of injectable decorator (#31573) 2019-07-16 23:57:50 -04:00
cli-builder.md docs: add filename headers to code examples (#32701) 2019-09-30 10:28:20 -07:00
comparing-observables.md docs: rewrite Observable examples with pipable operators (#31074) 2019-06-18 09:47:06 -07:00
complex-animation-sequences.md docs: correct description of animation example (#32009) 2019-08-13 11:16:31 -07:00
component-interaction.md feat(docs-infra): implement figure styles (#33259) 2019-10-23 12:59:34 -07:00
component-styles.md refactor(docs-infra): remove `linenums=false` since it is now the default (#31674) 2019-07-24 14:38:54 -07:00
creating-libraries.md docs: Update link (#33166) 2019-10-15 16:52:31 +00:00
dependency-injection-in-action.md feat(docs-infra): implement figure styles (#33259) 2019-10-23 12:59:34 -07:00
dependency-injection-navtree.md feat(docs-infra): implement figure styles (#33259) 2019-10-23 12:59:34 -07:00
dependency-injection-providers.md refactor(docs-infra): remove `linenums=false` since it is now the default (#31674) 2019-07-24 14:38:54 -07:00
dependency-injection.md docs: clarify hierarchical injectors (#28700) 2019-08-15 12:43:51 -07:00
deployment.md feat(docs-infra): implement figure styles (#33259) 2019-10-23 12:59:34 -07:00
deprecations.md docs: deprecate esm5 and fesm5 code distribution in our npm packages (#33360) 2019-10-23 16:39:36 -07:00
displaying-data.md feat(docs-infra): implement figure styles (#33259) 2019-10-23 12:59:34 -07:00
docs-style-guide.md docs: emphasize headers for code examples (#32938) 2019-10-01 14:45:29 -07:00
dynamic-component-loader.md feat(docs-infra): implement figure styles (#33259) 2019-10-23 12:59:34 -07:00
dynamic-form.md feat(docs-infra): implement figure styles (#33259) 2019-10-23 12:59:34 -07:00
elements.md feat(docs-infra): implement figure styles (#33259) 2019-10-23 12:59:34 -07:00
entry-components.md docs: remove prerequisite section in NgModules docs (#31169) 2019-07-17 13:26:00 -04:00
feature-modules.md feat(docs-infra): implement figure styles (#33259) 2019-10-23 12:59:34 -07:00
file-structure.md refactor(docs-infra): remove `linenums=false` since it is now the default (#31674) 2019-07-24 14:38:54 -07:00
form-validation.md refactor(docs-infra): remove `linenums=false` since it is now the default (#31674) 2019-07-24 14:38:54 -07:00
forms-overview.md feat(docs-infra): implement figure styles (#33259) 2019-10-23 12:59:34 -07:00
forms.md feat(docs-infra): implement figure styles (#33259) 2019-10-23 12:59:34 -07:00
frequent-ngmodules.md feat(docs-infra): implement figure styles (#33259) 2019-10-23 12:59:34 -07:00
glossary.md refactor(docs-infra): remove obsolete `format` attribute from code examples (#31674) 2019-07-24 14:38:54 -07:00
hierarchical-dependency-injection.md feat(docs-infra): implement figure styles (#33259) 2019-10-23 12:59:34 -07:00
http.md refactor(core): Migrate TestBed.get to TestBed.inject (#32382) 2019-09-09 19:10:54 -04:00
i18n.md docs: use neutral domain name in i18n example (#33146) 2019-10-15 16:50:01 +00:00
ivy.md docs: add postinstall script removal to Ivy guide (#32999) 2019-10-15 17:54:39 +00:00
language-service.md feat(docs-infra): implement figure styles (#33259) 2019-10-23 12:59:34 -07:00
lazy-loading-ngmodules.md feat(docs-infra): implement figure styles (#33259) 2019-10-23 12:59:34 -07:00
libraries.md docs: add custom-element ref to library overview (#28476) 2019-04-01 12:04:06 -07:00
lifecycle-hooks.md feat(docs-infra): implement figure styles (#33259) 2019-10-23 12:59:34 -07:00
migration-dynamic-flag.md docs: add dynamic queries flag migration documentation (#32582) 2019-10-01 17:19:05 -07:00
migration-injectable.md docs: add Injectable migration docs (#32581) 2019-10-11 15:46:12 -07:00
migration-localize.md docs: update localize migration doc (#33275) 2019-10-23 09:10:01 -07:00
migration-module-with-providers.md docs: add ModuleWithProviders migration doc (#33085) 2019-10-22 12:00:44 -07:00
migration-ngcc.md docs: add ngcc postinstall migration doc (#33328) 2019-10-22 14:26:15 -07:00
migration-renderer.md docs: move renderer2 deprecation guide into own file (#32626) 2019-09-12 10:42:59 -07:00
migration-undecorated-classes.md docs: add field decorators to undecorated classes migration (#33069) 2019-10-18 16:31:10 -04:00
module-types.md docs: remove prerequisite section in NgModules docs (#31169) 2019-07-17 13:26:00 -04:00
ngcc.md docs: add ModuleWithProviders migration doc (#33085) 2019-10-22 12:00:44 -07:00
ngmodule-api.md docs: remove prerequisite section in NgModules docs (#31169) 2019-07-17 13:26:00 -04:00
ngmodule-faq.md refactor(docs-infra): remove `linenums=false` since it is now the default (#31674) 2019-07-24 14:38:54 -07:00
ngmodule-vs-jsmodule.md docs: remove prerequisite section in NgModules docs (#31169) 2019-07-17 13:26:00 -04:00
ngmodules.md refactor(docs-infra): remove `linenums=false` since it is now the default (#31674) 2019-07-24 14:38:54 -07:00
npm-packages.md docs: move old quick start content into new local setup guide (#29651) 2019-05-20 10:16:23 -07:00
observables-in-angular.md docs: correct description of output decorator and add links to guide (#31780) 2019-08-08 12:15:29 -07:00
observables.md fix(docs-infra): rename "title" by "header" to avoid unwanted tooltips (#26396) 2018-10-17 11:05:29 -07:00
pipes.md feat(docs-infra): implement figure styles (#33259) 2019-10-23 12:59:34 -07:00
practical-observable-usage.md fix(docs-infra): rename "title" by "header" to avoid unwanted tooltips (#26396) 2018-10-17 11:05:29 -07:00
providers.md docs: expand abbreviation in providers guide (#32400) 2019-09-05 18:59:55 -04:00
reactive-forms.md feat(docs-infra): implement figure styles (#33259) 2019-10-23 12:59:34 -07:00
releases.md docs: add core and cli version alignment note (#30976) 2019-06-19 08:49:24 -07:00
reusable-animations.md refactor(docs-infra): remove `linenums=false` since it is now the default (#31674) 2019-07-24 14:38:54 -07:00
route-animations.md feat(docs-infra): implement figure styles (#33259) 2019-10-23 12:59:34 -07:00
router.md feat(docs-infra): implement figure styles (#33259) 2019-10-23 12:59:34 -07:00
rx-library.md docs: update rxjs refs (#31780) 2019-08-08 12:15:29 -07:00
schematics-authoring.md feat(docs-infra): implement figure styles (#33259) 2019-10-23 12:59:34 -07:00
schematics-for-libraries.md refactor(docs-infra): remove `linenums=false` since it is now the default (#31674) 2019-07-24 14:38:54 -07:00
schematics.md refactor(docs-infra): remove `linenums=false` since it is now the default (#31674) 2019-07-24 14:38:54 -07:00
security.md feat(docs-infra): implement figure styles (#33259) 2019-10-23 12:59:34 -07:00
service-worker-communications.md refactor(docs-infra): remove `linenums=false` since it is now the default (#31674) 2019-07-24 14:38:54 -07:00
service-worker-config.md feat(service-worker): remove deprecated `versionedFiles` option (#32862) 2019-10-02 09:30:59 -07:00
service-worker-devops.md docs: clarrify use of sw behind redirect (#32915) 2019-10-21 16:50:52 -04:00
service-worker-getting-started.md feat(docs-infra): implement figure styles (#33259) 2019-10-23 12:59:34 -07:00
service-worker-intro.md docs: reference articles specifically (#32221) 2019-08-26 15:03:04 -07:00
set-document-title.md feat(docs-infra): implement figure styles (#33259) 2019-10-23 12:59:34 -07:00
setup-local.md feat(docs-infra): implement figure styles (#33259) 2019-10-23 12:59:34 -07:00
sharing-ngmodules.md docs: remove prerequisite section in NgModules docs (#31169) 2019-07-17 13:26:00 -04:00
singleton-services.md docs: Fix typo in singleton-services.md page (#32330) 2019-08-27 09:07:18 -07:00
static-query-migration.md docs: fix typo in static query migration guide (#31572) 2019-07-15 16:46:33 -04:00
structural-directives.md feat(docs-infra): implement figure styles (#33259) 2019-10-23 12:59:34 -07:00
styleguide.md docs: clarify pipe naming (#31806) 2019-08-09 10:45:49 -07:00
template-syntax.md feat(docs-infra): implement figure styles (#33259) 2019-10-23 12:59:34 -07:00
testing.md feat(docs-infra): implement figure styles (#33259) 2019-10-23 12:59:34 -07:00
transition-and-triggers.md feat(docs-infra): implement figure styles (#33259) 2019-10-23 12:59:34 -07:00
typescript-configuration.md docs: removed duplicate "compilerOptions" property (#32449) 2019-09-03 11:57:32 -07:00
universal.md docs: update universal docs for new ivy implementation (#33040) 2019-10-21 11:25:44 -04:00
updating-to-version-9.md docs: deprecate esm5 and fesm5 code distribution in our npm packages (#33360) 2019-10-23 16:39:36 -07:00
updating.md docs: update deprecation practices, update release date, edits for readability and consistency (#29644) 2019-04-24 17:00:23 -07:00
upgrade-performance.md docs(upgrade): position old setup guide as legacy until it can be removed (#32193) 2019-08-21 15:20:48 -07:00
upgrade-setup.md docs(upgrade): position old setup guide as legacy until it can be removed (#32193) 2019-08-21 15:20:48 -07:00
upgrade.md feat(docs-infra): implement figure styles (#33259) 2019-10-23 12:59:34 -07:00
user-input.md feat(docs-infra): implement figure styles (#33259) 2019-10-23 12:59:34 -07:00
using-libraries.md refactor(docs-infra): remove obsolete `format` attribute from code examples (#31674) 2019-07-24 14:38:54 -07:00
visual-studio-2015.md docs: update node and typescript versions mentioned in docs (#30601) 2019-05-22 16:17:41 -07:00
web-worker.md docs: add header to code examples (#32563) 2019-09-11 19:26:19 -04:00
workspace-config.md docs: fix typo in Workspace Config guide code snippet (sourceMaps --> sourceMap) (#31957) 2019-08-05 12:57:18 -07:00