Symbols

What are Symbols?

Symbols are a new primitive data type available in ES6. With Symbol() we can create a new value that’s not equal to any other value, which is important when avoiding collisions is critical as when dealing with an IoC container.

How are Symbols used in Masquerade?

Each Service Provider should be registered with a unique Symbol to ensure the global integrity of any resolution calls.
This is achieved by exporting a single constant referencing the Symbol in our symbols.ts file.
1
export const Foo: unique symbol = Symbol.for("FooProvider")
Copied!
This symbol should be imported when manually resolving your service from the container.
1
import * as Symbols from "../symbols"
2
3
const foo: FooContract = container.resolve<FooContract>(Symbols.Foo)
Copied!
Although the container accepts strings as resolution tokens, we highly recommend defining a Symbol and using it in this way to avoid collisions.