# @autoinject and @optional

**URL:** <https://discourse.aurelia.io/t/autoinject-and-optional/3706>\
**Category:** Uncategorized\
**Created:** [August 11, 2020, 9:02am UTC](https://discourse.aurelia.io/t/autoinject-and-optional/3706 "2020-08-11T09:02:10Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![elitastic](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/elitastic/32/1787_2.png) [@elitastic](https://discourse.aurelia.io/u/elitastic)\
**Post date:** [August 11, 2020, 9:02am UTC](https://discourse.aurelia.io/t/autoinject-and-optional/3706/1 "2020-08-11T09:02:10Z")

</div>

Is it allowed to use @autoinject and @optional together?If yes, is this the correct syntax?

```
@autoinject
export class MyService implements IMyService
{
	constructor(
		@optional()
		private dependency: IOptionalDependency)
	{
	}
}

```

This leads to the following error: key/value cannot be null or undefined. Are you trying to inject/register something that doesn’t exist with DI

---

<div class="post-metadata">

**Author:** ![zewa666](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/zewa666/32/19_2.png) [@zewa666](https://discourse.aurelia.io/u/zewa666)\
**Post date:** [August 11, 2020, 10:54am UTC](https://discourse.aurelia.io/t/autoinject-and-optional/3706/2 "2020-08-11T10:54:42Z")

</div>

Are you trying to use an interface as type there? Since that resolves to nothing after compilation that might cause your troubles.

---

<div class="post-metadata">

**Author:** ![elitastic](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/elitastic/32/1787_2.png) [@elitastic](https://discourse.aurelia.io/u/elitastic)\
**Post date:** [August 11, 2020, 11:02am UTC](https://discourse.aurelia.io/t/autoinject-and-optional/3706/3 "2020-08-11T11:02:28Z")

</div>

No, it’s an abstract class!

```
public abstract class IOptionalDependency
{
    // ...
}
```

---

<div class="post-metadata">

**Author:** ![airboss001](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/airboss001/32/719_2.png) [@airboss001](https://discourse.aurelia.io/u/airboss001)\
**Post date:** [August 11, 2020, 12:00pm UTC](https://discourse.aurelia.io/t/autoinject-and-optional/3706/4 "2020-08-11T12:00:02Z")

</div>

@elitastic So I hope you don’t mind me asking a separate question, but related I think for my own knowledge purposes.

What does @optional provide over a default parameter value?  
Wouldn’t this be equivalent and less boilerplate? (no import, etc…)

`constructor(private dependency: IOptionalDependency = undefined)`

---

<div class="post-metadata">

**Author:** ![zewa666](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/zewa666/32/19_2.png) [@zewa666](https://discourse.aurelia.io/u/zewa666)\
**Post date:** [August 11, 2020, 12:43pm UTC](https://discourse.aurelia.io/t/autoinject-and-optional/3706/5 "2020-08-11T12:43:11Z")

</div>

Optional makes sure that only an injection happens if the corresponding key is already in the container. So in your case the autoinject would eagerly resolve and the default param is useless

Edit: sry was meant as a reply to @airboss001

---

<div class="post-metadata">

**Author:** ![airboss001](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/airboss001/32/719_2.png) [@airboss001](https://discourse.aurelia.io/u/airboss001)\
**Post date:** [August 11, 2020, 12:57pm UTC](https://discourse.aurelia.io/t/autoinject-and-optional/3706/6 "2020-08-11T12:57:21Z")

</div>

So, just for my own clarity, and restating it to make sure I have it right.

The container is the Aurelia default DI container.  
@optional will only use an existing DI container object, and would not instantiate a new one.  
@autoinject would have replaced any default constructor parameter values and they would never be seen. So if you actually wanted some default values, you would not want to use @autoinject.

How does that work in Aurelia 2 with its constructor having DI without using @autoinject?

---

<div class="post-metadata">

**Author:** ![zewa666](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/zewa666/32/19_2.png) [@zewa666](https://discourse.aurelia.io/u/zewa666)\
**Post date:** [August 11, 2020, 4:23pm UTC](https://discourse.aurelia.io/t/autoinject-and-optional/3706/7 "2020-08-11T16:23:11Z")

</div>

From the docs (v1) [Dependency Injection: Basics | Aurelia](https://aurelia.io/docs/fundamentals/dependency-injection#resolvers)

> If using TypeScript, keep in mind that `@autoinject` won’t allow you to use `Resolvers` (that is optional, lazy and others)

---

<div class="post-metadata">

**Author:** ![zewa666](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/zewa666/32/19_2.png) [@zewa666](https://discourse.aurelia.io/u/zewa666)\
**Post date:** [August 11, 2020, 4:30pm UTC](https://discourse.aurelia.io/t/autoinject-and-optional/3706/8 "2020-08-11T16:30:52Z")

</div>

Autoinject merely makes sure that metadata is present with the emitMetadata tsconfig for the given class. Actually any decorator will do so, see v2 docs  
[https://docs.aurelia.io/app-basics/dependency-injection](https://docs.aurelia.io/app-basics/dependency-injection)

> Note The secret of TypeScript’s metadata generation is that _any_ decorator on the class will cause the compiler to include the metadata. You don’t have to use the `inject` decorator specifically.

In v2 it should behave similarly with same drawbacks of no resolvers in this case. There are new benefits of being able to use Interfaces as keys though, through use of declaration merges with Symbols, which in itself is really a killer feature

---

<div class="post-metadata">

**Author:** ![airboss001](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/airboss001/32/719_2.png) [@airboss001](https://discourse.aurelia.io/u/airboss001)\
**Post date:** [August 11, 2020, 6:11pm UTC](https://discourse.aurelia.io/t/autoinject-and-optional/3706/9 "2020-08-11T18:11:22Z")

</div>

![inoveryourhead-small](https://canada1.discourse-cdn.com/flex027/uploads/aurelia/original/2X/f/f19c3c10c2235277b9b92f54e5b606e9b430974b.jpeg)  
Right, right, for sure, ummm…totally agree.

---

<div class="post-metadata">

**Author:** ![elitastic](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/elitastic/32/1787_2.png) [@elitastic](https://discourse.aurelia.io/u/elitastic)\
**Post date:** [August 11, 2020, 6:22pm UTC](https://discourse.aurelia.io/t/autoinject-and-optional/3706/10 "2020-08-11T18:22:01Z")

</div>

Thanks for all your great help!
