question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Container resolve does work anymore with default parameters since v4.4

See original GitHub issue

Describe the bug DI does not work properly when using default values.

Error: Cannot inject the dependency at position #0 of "Target" constructor. Reason:
    TypeInfo not known for "Object"

To Reproduce Build and run main.

import "reflect-metadata";
import {container, injectable} from "tsyringe";

@injectable()
class Target {
  constructor(subject = "hello world") {}
}

function main() {
    const t = container.resolve(Target);
}

Expected behavior t correctly resolved with t.subject == “hello world”

Version: tsyringe v4.4

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:3
  • Comments:18

github_iconTop GitHub Comments

2reactions
RaulGF92commented, Mar 25, 2022

Yeah, having issues with my @singleton class:

import { singleton } from 'tsyringe';

@singleton()
export class Config {
  public readonly region: string;
  constructor(
    region?: string,
  ) {
    this.region = region || process.env.REGION || 'localhost';
  }
}

Basically I need the optional value outright ignored outside of testing, unit tests call the Config file with the constructor and add it to the container.

That is your solution using Tsyring but if you need to create runtime Config instances I don’t recommend you use singleton or Tsyringe, use a simple class.

import { singleton, inject } from 'tsyringe';

container.register('ConfigRegion', {
    useValue: process.env.REGION || 'localhost'
});

@singleton()
export class Config {
  public readonly region: string;
  constructor(
    @inject("ConfigRegion") region?: string,
  ) {
    this.region = region;
  }
}
1reaction
RaulGF92commented, Jan 20, 2022

Why is it closed but it’s not the best approach?

This is how I solved the problem indeed this solution is in the part of documentation…

https://github.com/microsoft/tsyringe#injecting-primitive-values-named-injection

import { container, inject, injectable } from 'tsyringe';
import config from 'config';
import AWS from 'aws-sdk';

container.register('SqsClientQueueUrl', {
    useValue: <string> config.get('cu-enablement-queue-url')
});

@injectable()
export default class SqsClient {
    constructor(
        private sqs: AWS.SQS = new AWS.SQS(),
        @inject('SqsClientQueueUrl') private queueUrl: string
    ) {
    }

    sendMessage(message) {
        const params = {
            MessageBody: JSON.stringify(message),
            QueueUrl: <string> this.queueUrl
        };
        return this.sqs.sendMessage(params).promise();
    }
}

But I don’t think this is the best approach because you are forced to make an extern initialization… Hopefully, the dev team will implement the logical behaviour and we won’t need to use that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Registration Concepts — Autofac 6.0.0 documentation
You register components with Autofac by creating a ContainerBuilder and informing the builder which components expose which services. Components can be ...
Read more >
"Least Astonishment" and the Mutable Default Argument
When Python encounters it, the first thing it will do is compile it in order to create a code object for this function....
Read more >
Troubleshoot issues when using Azure Functions trigger for ...
Common issues, workarounds, and diagnostic steps, when using the Azure Functions trigger for Azure Cosmos DB.
Read more >
Change log - PHP-DI
#343 Autowiring and Annotations do not work for object() inside arrays: it ... #223: DI\ContainerInterface was deprecated since v4.1 and has been removed....
Read more >
Docker Desktop release notes - Docker Documentation
Fixed container DNS lookups when using Registry Access Management. For Mac. Fixed an issue preventing the Analyze Image button on the Images tab...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found