Skip to content

Instantly share code, notes, and snippets.

@synchrone
Last active July 11, 2019 12:32
Show Gist options
  • Save synchrone/0fe69a1680fd686e5438b13a4c8d2134 to your computer and use it in GitHub Desktop.
Save synchrone/0fe69a1680fd686e5438b13a4c8d2134 to your computer and use it in GitHub Desktop.
TypeDI Inject conflict repro
import 'reflect-metadata'
import {Container, Inject, Service} from "typedi";
@Service()
export class Bla {}
const testconfig = {}
type ConfigType = typeof testconfig
@Service()
export class Base {
public constructor(
@Inject('config')
protected cfg: ConfigType, //This is not injected
protected bla: Bla // if this is coming from a module w/ import 'reflect-metadata'
) {
if(!cfg){
throw Error('expected typedi to inject cfg')
}
console.log('cfg injected')
}
}
@Service()
export class Child extends Base {}
if (require.main === module) {
Container.set('config', testconfig)
Container.get(Base) //works
Container.get(Child) //inherited constructor does not receive a named dependency
}
{
"name": "bug-repro",
"version": "0.0.0",
"private": true,
"main": "dist/index.js",
"types": "./",
"scripts": {
"start": "tsc && node dist/index.js"
},
"dependencies": {
"typedi": "^0.8.0"
},
"devDependencies": {
"typescript": "^3.4.3"
}
}
{
"compilerOptions": {
/* Basic Options */
"target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"allowJs": false, /* Allow javascript files to be compiled. */
"sourceMap": true, /* Generates corresponding '.map' file. */
"incremental": true, /* Enable incremental compilation by reading/writing information from prior compilations to a file on disk.*/
"outDir": "dist", /* Redirect output structure to the directory. */
"declaration": true, /* Generates corresponding .d.ts file */
"removeComments": true, /* Do not emit comments to output. */
/* Strict Type-Checking Options */
"noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
"strictNullChecks": true, /* Enable strict null checks. */
"noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
/* Additional Checks */
"noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
"forceConsistentCasingInFileNames": true, /* Disallow inconsistently-cased references to the same file. */
/* Module Resolution Options */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
"allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
/* Experimental Options */
"experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
"emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
},
"exclude": [
"dist",
"node_modules",
"**/*.spec.ts",
"**/*.test.ts",
"**/*.d.ts"
],
"include": ["./**/*"]
}
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
typedi@^0.8.0:
version "0.8.0"
resolved "https://registry.yarnpkg.com/typedi/-/typedi-0.8.0.tgz#d8e203bd1d41a96e2b0a5c6295147d74b2b2d03e"
integrity sha512-/c7Bxnm6eh5kXx2I+mTuO+2OvoWni5+rXA3PhXwVWCtJRYmz3hMok5s1AKLzoDvNAZqj/Q/acGstN0ri5aQoOA==
typescript@^3.4.3:
version "3.5.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.5.3.tgz#c830f657f93f1ea846819e929092f5fe5983e977"
integrity sha512-ACzBtm/PhXBDId6a6sDJfroT2pOWt/oOnk4/dElG5G33ZL776N3Y6/6bKZJBFpd+b05F3Ct9qDjMeJmRWtE2/g==
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment