thiagobustamante / typescript-ioc

A Lightweight annotation-based dependency injection container for typescript.
MIT License
526 stars 64 forks source link

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

Closed adamdry closed 6 years ago

adamdry commented 6 years ago

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): https://github.com/adamdry/node-di-mocha-test/commit/120475877fbc08af0ed436560ce4e735c4b450c4

To run the demo: npm i npm run dev

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

Thanks.

thiagobustamante commented 6 years ago

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 commented 6 years ago

Thanks, this is indeed now fixed! :+1:

theanurin commented 4 years ago

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...