Last active
September 24, 2020 11:23
-
-
Save xesjkeee/b94bca7ca86c91bbd05de806aff5d01b to your computer and use it in GitHub Desktop.
async-redis: Add export interface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For example, I use Dependency Injection pattern in my architecture of node.js app | |
import asyncRedis from 'async-redis' | |
import { Container } from 'typedi' | |
const client = asyncRedis.createClient(config.redis) | |
Container.set('redis', client) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Service, Inject } from 'typedi' | |
@Service() | |
export default class Animal { | |
constructor ( | |
@Inject('redis') private redis: any // <-- problem here, i can't set type of redis client | |
) {} | |
async getAll () { | |
// this.redis is any and set-method is too any | |
await this.redis.set('foo', 'bar') | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// What I expecting | |
import { Service, Inject } from 'typedi' | |
import { Promisified, RedisClient } from 'async-redis' | |
@Service() | |
export default class Animal { | |
constructor ( | |
@Inject('redis') private redis: Promisified<RedisClient> // <-- here i can validate type-chicking | |
) {} | |
async getAll () { | |
await this.redis.set('foo', 'bar') | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment