{ "id": "guide/aot-metadata-errors", "title": "AOT metadata errors", "contents": "\n\n\n
The following are metadata errors you may encounter, with explanations and suggested corrections.
\nExpression 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
The compiler encountered an expression it didn't understand while evaluating Angular metadata.
\nLanguage features outside of the compiler's restricted expression syntax\ncan produce this error, as seen in the following example:
\nYou can use typeof
and bracket notation in normal application code.\nYou just can't use those features within expressions that define Angular metadata.
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\nReference to a local (non-exported) symbol 'symbol name'. Consider exporting the symbol.
\nThe compiler encountered a referenced to a locally defined symbol that either wasn't exported or wasn't initialized.
\nHere's a provider
example of the problem.
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.
You could fix the problem by initializing foo
.
The compiler will fold the expression into the provider as if you had written this.
\nAlternatively, you can fix it by exporting foo
with the expectation that foo
will be assigned at runtime when you actually know its value.
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.
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.
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
\".
Only initialized variables and constants can be referenced because the value of this variable is needed by the template compiler.
\nThe 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.
\nThe following example tries to set the component's template
property to the value of\nthe exported someTemplate
variable which is declared but unassigned.
You'd also get this error if you imported someTemplate
from some other module and neglected to initialize it there.
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.
To correct this error, provide the initial value of the variable in an initializer clause on the same line.
\nReference to a non-exported class
Metadata referenced a class that wasn't exported.
\nFor example, you may have defined a class and used it as an injection token in a providers array\nbut neglected to export that class.
\nAngular generates a class factory in a separate module and that\nfactory can only access exported classes.\nTo correct this error, export the referenced class.
\nMetadata referenced a function that wasn't exported.
\nFor example, you may have set a providers useFactory
property to a locally defined function that you neglected to export.
Angular generates a class factory in a separate module and that\nfactory can only access exported functions.\nTo correct this error, export the function.
\nFunction calls are not supported. Consider replacing the function or lambda with a reference to an exported function.
\nThe 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.
You also get this error if you call a function or method in a provider's useValue
.
To correct this error, export a function from the module and refer to the function in a useFactory
provider instead.
Referencing an exported destructured variable or constant is not supported by the template compiler. Consider simplifying this to avoid destructuring.
\nThe compiler does not support references to variables assigned by destructuring.
\nFor example, you cannot write something like this:
\nTo correct this error, refer to non-destructured values.
\nThe compiler encountered a type and can't determine which module exports that type.
\nThis 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.
You'll get an error if you reference it in the component constructor,\nwhich the compiler must statically analyze.
\nTypeScript understands ambient types so you don't import them.\nThe Angular compiler does not understand a type that you neglect to export or import.
\nIn this case, the compiler doesn't understand how to inject something with the Window
token.
Do not refer to ambient types in metadata expressions.
\nIf you must inject an instance of an ambient type,\nyou can finesse the problem in four steps:
\nuseFactory
provider with that factory function.@Inject
to inject the instance.Here's an illustrative example.
\nThe Window
type in the constructor is no longer a problem for the compiler because it\nuses the @Inject(WINDOW)
to generate the injection code.
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).
The compiler expected a name in an expression it was evaluating.
\nThis can happen if you use a number as a property name as in the following example.
\nChange the name of the property to something non-numeric.
\nAngular couldn't determine the value of the enum member that you referenced in metadata.
\nThe compiler can understand simple enum values but not complex values such as those derived from computed properties.
\nAvoid referring to enums with complicated initializers or computed properties.
\n\nTagged template expressions are not supported in metadata.
\nThe compiler encountered a JavaScript ES2015 tagged template expression such as the following.
\nString.raw()
\nis a tag function native to JavaScript ES2015.
The AOT compiler does not support tagged template expressions; avoid them in metadata expressions.
\n\nThe compiler expected a reference to a symbol at the location specified in the error message.
\nThis error can occur if you use an expression in the extends
clause of a class.