{ "id": "guide/aot-metadata-errors", "title": "AOT metadata errors", "contents": "\n\n\n
\n mode_edit\n
\n\n\n
\n

AOT metadata errorslink

\n

The following are metadata errors you may encounter, with explanations and suggested corrections.

\n

Expression form not supported
\nReference to a local (non-exported) symbol
\nOnly initialized variables and constants
\nReference to a non-exported class
\nReference to a non-exported function
\nFunction calls are not supported
\nDestructured variable or constant not supported
\nCould not resolve type
\nName expected
\nUnsupported enum member name
\nTagged template expressions are not supported
\nSymbol reference expected

\n\n

Expression form not supportedlink

\n
\n

The compiler encountered an expression it didn't understand while evaluating Angular metadata.

\n
\n

Language features outside of the compiler's restricted expression syntax\ncan produce this error, as seen in the following example:

\n\n// ERROR\nexport class Fooish { ... }\n...\nconst prop = typeof Fooish; // typeof is not valid in metadata\n ...\n // bracket notation is not valid in metadata\n { provide: 'token', useValue: { [prop]: 'value' } };\n ...\n\n

You can use typeof and bracket notation in normal application code.\nYou just can't use those features within expressions that define Angular metadata.

\n

Avoid this error by sticking to the compiler's restricted expression syntax\nwhen writing Angular metadata\nand be wary of new or unusual TypeScript features.

\n\n

Reference to a local (non-exported) symbollink

\n
\n

Reference to a local (non-exported) symbol 'symbol name'. Consider exporting the symbol.

\n
\n

The compiler encountered a referenced to a locally defined symbol that either wasn't exported or wasn't initialized.

\n

Here's a provider example of the problem.

\n\n// ERROR\nlet foo: number; // neither exported nor initialized\n\n@Component({\n selector: 'my-component',\n template: ... ,\n providers: [\n { provide: Foo, useValue: foo }\n ]\n})\nexport class MyComponent {}\n\n

The compiler generates the component factory, which includes the useValue provider code, in a separate module. That factory module can't reach back to this source module to access the local (non-exported) foo variable.

\n

You could fix the problem by initializing foo.

\n\nlet foo = 42; // initialized\n\n

The compiler will fold the expression into the provider as if you had written this.

\n\n providers: [\n { provide: Foo, useValue: 42 }\n ]\n\n

Alternatively, you can fix it by exporting foo with the expectation that foo will be assigned at runtime when you actually know its value.

\n\n// CORRECTED\nexport let foo: number; // exported\n\n@Component({\n selector: 'my-component',\n template: ... ,\n providers: [\n { provide: Foo, useValue: foo }\n ]\n})\nexport class MyComponent {}\n\n

Adding export often works for variables referenced in metadata such as providers and animations because the compiler can generate references to the exported variables in these expressions. It doesn't need the values of those variables.

\n

Adding export doesn't work when the compiler needs the actual value\nin order to generate code.\nFor example, it doesn't work for the template property.

\n\n// ERROR\nexport let someTemplate: string; // exported but not initialized\n\n@Component({\n selector: 'my-component',\n template: someTemplate\n})\nexport class MyComponent {}\n\n

The compiler needs the value of the template property right now to generate the component factory.\nThe variable reference alone is insufficient.\nPrefixing the declaration with export merely produces a new error, \"Only initialized variables and constants can be referenced\".

\n\n

Only initialized variables and constantslink

\n
\n

Only initialized variables and constants can be referenced because the value of this variable is needed by the template compiler.

\n
\n

The compiler found a reference to an exported variable or static field that wasn't initialized.\nIt needs the value of that variable to generate code.

\n

The following example tries to set the component's template property to the value of\nthe exported someTemplate variable which is declared but unassigned.

\n\n// ERROR\nexport let someTemplate: string;\n\n@Component({\n selector: 'my-component',\n template: someTemplate\n})\nexport class MyComponent {}\n\n

You'd also get this error if you imported someTemplate from some other module and neglected to initialize it there.

\n\n// ERROR - not initialized there either\nimport { someTemplate } from './config';\n\n@Component({\n selector: 'my-component',\n template: someTemplate\n})\nexport class MyComponent {}\n\n

The compiler cannot wait until runtime to get the template information.\nIt must statically derive the value of the someTemplate variable from the source code\nso that it can generate the component factory, which includes\ninstructions for building the element based on the template.

\n

To correct this error, provide the initial value of the variable in an initializer clause on the same line.

\n\n// CORRECTED\nexport let someTemplate = '<h1>Greetings from Angular</h1>';\n\n@Component({\n selector: 'my-component',\n template: someTemplate\n})\nexport class MyComponent {}\n\n\n

Reference to a non-exported classlink

\n
\n

Reference to a non-exported class . Consider exporting the class.

\n
\n

Metadata referenced a class that wasn't exported.

\n

For example, you may have defined a class and used it as an injection token in a providers array\nbut neglected to export that class.

\n\n// ERROR\nabstract class MyStrategy { }\n\n ...\n providers: [\n { provide: MyStrategy, useValue: ... }\n ]\n ...\n\n

Angular generates a class factory in a separate module and that\nfactory can only access exported classes.\nTo correct this error, export the referenced class.

\n\n// CORRECTED\nexport abstract class MyStrategy { }\n\n ...\n providers: [\n { provide: MyStrategy, useValue: ... }\n ]\n ...\n\n\n

Reference to a non-exported functionlink

\n
\n

Metadata referenced a function that wasn't exported.

\n
\n

For example, you may have set a providers useFactory property to a locally defined function that you neglected to export.

\n\n// ERROR\nfunction myStrategy() { ... }\n\n ...\n providers: [\n { provide: MyStrategy, useFactory: myStrategy }\n ]\n ...\n\n

Angular generates a class factory in a separate module and that\nfactory can only access exported functions.\nTo correct this error, export the function.

\n\n// CORRECTED\nexport function myStrategy() { ... }\n\n ...\n providers: [\n { provide: MyStrategy, useFactory: myStrategy }\n ]\n ...\n\n\n

Function calls are not supportedlink

\n
\n

Function calls are not supported. Consider replacing the function or lambda with a reference to an exported function.

\n
\n

The compiler does not currently support function expressions or lambda functions.\nFor example, you cannot set a provider's useFactory to an anonymous function or arrow function like this.

\n\n// ERROR\n ...\n providers: [\n { provide: MyStrategy, useFactory: function() { ... } },\n { provide: OtherStrategy, useFactory: () => { ... } }\n ]\n ...\n\n

You also get this error if you call a function or method in a provider's useValue.

\n\n// ERROR\nimport { calculateValue } from './utilities';\n\n ...\n providers: [\n { provide: SomeValue, useValue: calculateValue() }\n ]\n ...\n\n

To correct this error, export a function from the module and refer to the function in a useFactory provider instead.

\n\n// CORRECTED\nimport { calculateValue } from './utilities';\n\nexport function myStrategy() { ... }\nexport function otherStrategy() { ... }\nexport function someValueFactory() {\n return calculateValue();\n}\n ...\n providers: [\n { provide: MyStrategy, useFactory: myStrategy },\n { provide: OtherStrategy, useFactory: otherStrategy },\n { provide: SomeValue, useFactory: someValueFactory }\n ]\n ...\n\n\n

Destructured variable or constant not supportedlink

\n
\n

Referencing an exported destructured variable or constant is not supported by the template compiler. Consider simplifying this to avoid destructuring.

\n
\n

The compiler does not support references to variables assigned by destructuring.

\n

For example, you cannot write something like this:

\n\n// ERROR\nimport { configuration } from './configuration';\n\n// destructured assignment to foo and bar\nconst {foo, bar} = configuration;\n ...\n providers: [\n {provide: Foo, useValue: foo},\n {provide: Bar, useValue: bar},\n ]\n ...\n\n

To correct this error, refer to non-destructured values.

\n\n// CORRECTED\nimport { configuration } from './configuration';\n ...\n providers: [\n {provide: Foo, useValue: configuration.foo},\n {provide: Bar, useValue: configuration.bar},\n ]\n ...\n\n\n

Could not resolve typelink

\n
\n

The compiler encountered a type and can't determine which module exports that type.

\n
\n

This can happen if you refer to an ambient type.\nFor example, the Window type is an ambient type declared in the global .d.ts file.

\n

You'll get an error if you reference it in the component constructor,\nwhich the compiler must statically analyze.

\n\n// ERROR\n@Component({ })\nexport class MyComponent {\n constructor (private win: Window) { ... }\n}\n\n

TypeScript understands ambient types so you don't import them.\nThe Angular compiler does not understand a type that you neglect to export or import.

\n

In this case, the compiler doesn't understand how to inject something with the Window token.

\n

Do not refer to ambient types in metadata expressions.

\n

If you must inject an instance of an ambient type,\nyou can finesse the problem in four steps:

\n
    \n
  1. Create an injection token for an instance of the ambient type.
  2. \n
  3. Create a factory function that returns that instance.
  4. \n
  5. Add a useFactory provider with that factory function.
  6. \n
  7. Use @Inject to inject the instance.
  8. \n
\n

Here's an illustrative example.

\n\n// CORRECTED\nimport { Inject } from '@angular/core';\n\nexport const WINDOW = new InjectionToken('Window');\nexport function _window() { return window; }\n\n@Component({\n ...\n providers: [\n { provide: WINDOW, useFactory: _window }\n ]\n})\nexport class MyComponent {\n constructor (@Inject(WINDOW) private win: Window) { ... }\n}\n\n

The Window type in the constructor is no longer a problem for the compiler because it\nuses the @Inject(WINDOW) to generate the injection code.

\n

Angular does something similar with the DOCUMENT token so you can inject the browser's document object (or an abstraction of it, depending upon the platform in which the application runs).

\n\nimport { Inject } from '@angular/core';\nimport { DOCUMENT } from '@angular/common';\n\n@Component({ ... })\nexport class MyComponent {\n constructor (@Inject(DOCUMENT) private doc: Document) { ... }\n}\n\n\n

Name expectedlink

\n
\n

The compiler expected a name in an expression it was evaluating.

\n
\n

This can happen if you use a number as a property name as in the following example.

\n\n// ERROR\nprovider: [{ provide: Foo, useValue: { 0: 'test' } }]\n\n

Change the name of the property to something non-numeric.

\n\n// CORRECTED\nprovider: [{ provide: Foo, useValue: { '0': 'test' } }]\n\n\n

Unsupported enum member namelink

\n
\n

Angular couldn't determine the value of the enum member that you referenced in metadata.

\n
\n

The compiler can understand simple enum values but not complex values such as those derived from computed properties.

\n\n// ERROR\nenum Colors {\n Red = 1,\n White,\n Blue = \"Blue\".length // computed\n}\n\n ...\n providers: [\n { provide: BaseColor, useValue: Colors.White } // ok\n { provide: DangerColor, useValue: Colors.Red } // ok\n { provide: StrongColor, useValue: Colors.Blue } // bad\n ]\n ...\n\n

Avoid referring to enums with complicated initializers or computed properties.

\n\n

Tagged template expressions are not supportedlink

\n
\n

Tagged template expressions are not supported in metadata.

\n
\n

The compiler encountered a JavaScript ES2015 tagged template expression such as the following.

\n\n// ERROR\nconst expression = 'funky';\nconst raw = String.raw`A tagged template ${expression} string`;\n ...\n template: '<div>' + raw + '</div>'\n ...\n\n

String.raw()\nis a tag function native to JavaScript ES2015.

\n

The AOT compiler does not support tagged template expressions; avoid them in metadata expressions.

\n\n

Symbol reference expectedlink

\n
\n

The compiler expected a reference to a symbol at the location specified in the error message.

\n
\n

This error can occur if you use an expression in the extends clause of a class.

\n\n\n \n
\n\n\n" }