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

Invalid type requested to IoC container. Type is not defined. in node js app #19

Closed
adamdry opened this issue Dec 15, 2017 · 3 comments
Closed

Comments

@adamdry
Copy link

adamdry commented Dec 15, 2017

I have 2 simple classes and an index.ts entry script.

index.ts looks like this:

import ConfigHelper from './config/ConfigHelper'

const configHelper: ConfigHelper = new ConfigHelper()
configHelper.testOutput()
console.log(configHelper.fsAsyncFactory)

ConfigHelper looks like this:

import {AutoWired, Inject} from 'typescript-ioc'
import FsAsyncFactory from '../fs/FsAsyncFactory'

export class ConfigHelper {

    @Inject
    public fsAsyncFactory: FsAsyncFactory

    public testOutput(): void {
        console.log('### ConfigHelper.testOutput')
    }

}

export default ConfigHelper

FsAsyncFactory looks like this:

import * as BB from 'bluebird'
import * as fs from 'fs'

export class FsAsyncFactory {

    private fsAsync: any = null

    public getFsAsync(): any {

        if (this.fsAsync === null) {
            this.fsAsync = BB.promisifyAll(fs)
        }

        return this.fsAsync
    }

}

export default FsAsyncFactory

But when I run it I get an output of:

### ConfigHelper.testOutput
.../internalprojects/node-di-mocha-test/node_modules/typescript-ioc/es5.js:137
throw new TypeError('Invalid type requested to IoC ' +
^

TypeError: Invalid type requested to IoC container. Type is not defined.
at checkType (.../internalprojects/node-di-mocha-test/node_modules/typescript-ioc/es5.js:137:15)
at Function.IoCContainer.bind (.../internalprojects/node-di-mocha-test/node_modules/typescript-ioc/es5.js:98:9)
at Function.IoCContainer.get (.../internalprojects/node-di-mocha-test/node_modules/typescript-ioc/es5.js:108:35)
at ConfigHelper.get [as fsAsyncFactory] (.../internalprojects/node-di-mocha-test/node_modules/typescript-ioc/es5
                                                                                             .js:119:85)
at Object.<anonymous> (.../internalprojects/node-di-mocha-test/dist/index.js:5:25)
at Module._compile (module.js:624:30)
at Object.Module._extensions..js (module.js:635:10)
at Module.load (module.js:545:32)
at tryModuleLoad (module.js:508:12)
at Function.Module._load (module.js:500:3)

I have a public repo here if you'd like to try for yourself (use this specific commit): adamdry/node-di-mocha-test@1204758

To run the demo:
npm i
npm run dev

I imagine I'm doing something stupid though... :)

Thanks.

@thiagobustamante
Copy link
Owner

thiagobustamante commented Mar 15, 2018

Hi @adamdry,

I tried your code, but it worked as expected here. No error occurred. I saw that the version declared in package.json was 1.0.2. When I ran npm i, it installed the newest 1.1.1.

I don't know what caused the error to you, but can you try again with this new version?

@adamdry
Copy link
Author

adamdry commented Apr 29, 2018

Thanks, this is indeed now fixed! 👍

@adamdry adamdry closed this as completed Apr 29, 2018
@theanurin
Copy link

I met same error message when missing emitDecoratorMetadata: true in my tsconfig.json ("version": "1.2.5")

[2019-10-07T23:52:26.965] [FATAL] launcher - Invalid type requested to IoC container. Type is not defined. TypeError: Invalid type requested to IoC container. Type is not defined.
    at checkType (service/node_modules/typescript-ioc/src/typescript-ioc.ts:356:15)
    at Function.bind (service/node_modules/typescript-ioc/src/typescript-ioc.ts:301:9)
    at Function.get (service/node_modules/typescript-ioc/src/typescript-ioc.ts:312:49)
    at RuntimeImpl.get [as hostingService] (service/node_modules/typescript-ioc/src/typescript-ioc.ts:334:85)

I think(proposal) this case should be detected in runtime with a warning message.
@adamdry if you you agree with my proposal let me know and I will return with a merge request...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants