tsyringe icon indicating copy to clipboard operation
tsyringe copied to clipboard

inject optional parameters

Open Harshita-mindfire opened this issue 3 years ago • 0 comments

Extremely new to the DI tsyringe, how do we handle optional parameters with@inject decorator? With the below setup, I get the following error:

Cannot inject the dependency siteUtils in UtilsWeb, Reason: TypeInfo not known for "undefined"

My use case is : the siteUrl and enablePrettyLinks can have undefined values. They are being read from a .yml file

-------------
  container.register("siteUrl", { useValue: siteUrl }); // siteUrl can have undefined value
  container.register("enablePrettyLinks", { useValue: enablePrettyLinks }); // enablePrettyLinks can have undefined value
------------------

@injectable()
export class UtilsWeb {
  constructor(
    @inject("siteUrl") private siteUrl?: string,
    @inject("enablePrettyLinks") private enablePrettyLinks?: boolean
  ) {}
...
}

Harshita-mindfire avatar Aug 22 '22 12:08 Harshita-mindfire