Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: non-injected array class parameter throws error #530

Open
hoonoh opened this issue Oct 20, 2021 · 0 comments · May be fixed by #674
Open

fix: non-injected array class parameter throws error #530

hoonoh opened this issue Oct 20, 2021 · 0 comments · May be fixed by #674
Labels
status: has PR Issues which has a related PR closing the issue. status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature.

Comments

@hoonoh
Copy link

hoonoh commented Oct 20, 2021

Description

If non-injected array parameter exist in class constructor, TypeDI throws error.

Minimal code-snippet showcasing the problem

import 'reflect-metadata';

import Container, { Inject, Service, Token } from 'typedi';

const token = new Token('token');
Container.set(token, 'tokenValue');

@Service()
export class Test {
  constructor(
    @Inject(token) injected: string,
    // string array parameter
    notInjected?: string[],
  ) {
    console.log('injected:', injected);
    console.log('notInjected:', notInjected);
  }
}

Container.get(Test);
/*
.../node_modules/src/container-instance.class.ts:75
    throw new ServiceNotFoundError(identifier);
          ^
ServiceNotFoundError: Service with "MaybeConstructable<Array>" identifier was not found in the container. Register it before usage via explicitly calling the "Container.set" function or using the "@Service()" decorator.
    at ContainerInstance.get (.../node_modules/src/container-instance.class.ts:75:11)
    at .../node_modules/src/container-instance.class.ts:334:21
    at Array.map (<anonymous>)
    at ContainerInstance.initializeParams (.../node_modules/src/container-instance.class.ts:314:23)
    at ContainerInstance.getServiceValue (.../node_modules/src/container-instance.class.ts:277:27)
    at ContainerInstance.get (.../node_modules/src/container-instance.class.ts:54:36)
    at Function.get (.../node_modules/src/container.class.ts:61:32)
    at Object.<anonymous> (.../services/api/src/zz.ts:20:11)
    at Module._compile (internal/modules/cjs/loader.js:1072:14)
    at Module.m._compile (.../node_modules/ts-node/src/index.ts:1365:23) {
  normalizedIdentifier: 'MaybeConstructable<Array>'
}
*/

Expected behavior

If non-injected parameter is not an array, error is not thrown:

import 'reflect-metadata';

import Container, { Inject, Service, Token } from 'typedi';

const token = new Token('token');
Container.set(token, 'tokenValue');

@Service()
export class Test {
  constructor(
    @Inject(token) injected: string,
    // string parameter
    notInjected?: string,
  ) {
    console.log('injected:', injected);
    console.log('notInjected:', notInjected);
  }
}

Container.get(Test);
/*
injected: tokenValue
notInjected: undefined
*/

A workaround to above failure is to define non-injected parameter with union with another type.
For example, notInjected: string[] | undefined instead of notInjected?: string[]

@hoonoh hoonoh added status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature. labels Oct 20, 2021
@attilaorosz attilaorosz linked a pull request Mar 7, 2022 that will close this issue
6 tasks
@attilaorosz attilaorosz added the status: has PR Issues which has a related PR closing the issue. label Mar 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: has PR Issues which has a related PR closing the issue. status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature.
Development

Successfully merging a pull request may close this issue.

2 participants