简体   繁体   English

存在时类型上不存在属性

[英]Property does not exist on type when it exists

I have this class我有这个 class

import Redis = require('ioredis')
import { Mutex } from 'redis-semaphore'

const LOCKED_STREAM_IDS_REDIS_KEY = 'ghost_stream_merger_locked_stream_ids'
const NOT_FULL_STREAM_IDS_REDIS_KEY = 'ghost_stream_merger_not_full_stream_ids'
const MUTEX_NAME = 'ghost_stream_merger'

export interface IRedisCache {
  redisClient : any
  mutex : Mutex
  getLockedStreamIds() : Promise<string[]>
  addLockedStreamIds(streamIds : string[]) : Promise<number>
  removeLockedStreamIds(streamIds : string[]) : Promise<number>
  deleteLockedStreamIds() : Promise<number>
  getNotFullStreamIds() : Promise<string[]>
  addNotFullStreamIds(streamIds : string[]) : Promise<number>
  removeNotFullStreamIds(streamIds : string[]) : Promise<number>
  deleteNotFullStreamIds() : Promise<number>
}

export default class RedisCache implements IRedisCache {
  redisClient : any
  mutex : Mutex
  constructor(redisConfig : any) {
    this.redisClient = new Redis(redisConfig)
    this.mutex = new Mutex(this.redisClient, MUTEX_NAME)
  }
  
  async getLockedStreamIds() {
    return (await this.redisClient.smembers(LOCKED_STREAM_IDS_REDIS_KEY)) || []
  }
  
  async addLockedStreamIds(streamIds : string[]) {
    return this.redisClient.sadd(LOCKED_STREAM_IDS_REDIS_KEY, streamIds)
  }
  
  async removeLockedStreamIds(streamIds : string[]) {
    return this.redisClient.srem(LOCKED_STREAM_IDS_REDIS_KEY, streamIds)
  }
  
  async deleteLockedStreamIds() {
    return this.redisClient.del(LOCKED_STREAM_IDS_REDIS_KEY)
  }
  
  async getNotFullStreamIds() {
    return (await this.redisClient.smembers(NOT_FULL_STREAM_IDS_REDIS_KEY)) || []
  }
  
  async addNotFullStreamIds(streamIds : string[]) {
    return this.redisClient.sadd(NOT_FULL_STREAM_IDS_REDIS_KEY, streamIds)
  }
  
  async removeNotFullStreamIds(streamIds : string[]) {
    return this.redisClient.srem(NOT_FULL_STREAM_IDS_REDIS_KEY, streamIds)
  }
  
  async deleteNotFullStreamIds() {
    return this.redisClient.del(NOT_FULL_STREAM_IDS_REDIS_KEY)
  }
}

and when i trying to import it, i get this errors:当我尝试导入它时,出现以下错误:

TSError: ⨯ Unable to compile TypeScript:
src/lib/cache.ts:10:14 - error TS2339: Property 'redisClient' does not exist on type 'RedisCache'.

10         this.redisClient = new Redis(redisConfig);
                ~~~~~~~~~~~
src/lib/cache.ts:11:14 - error TS2339: Property 'mutex' does not exist on type 'RedisCache'.

11         this.mutex = new redis_semaphore_1.Mutex(this.redisClient, MUTEX_NAME);
                ~~~~~
src/lib/cache.ts:11:55 - error TS2339: Property 'redisClient' does not exist on type 'RedisCache'.

11         this.mutex = new redis_semaphore_1.Mutex(this.redisClient, MUTEX_NAME);
                                                         ~~~~~~~~~~~
src/lib/cache.ts:14:28 - error TS2339: Property 'redisClient' does not exist on type 'RedisCache'.

14         return (await this.redisClient.smembers(LOCKED_STREAM_IDS_REDIS_KEY)) || [];
                              ~~~~~~~~~~~
src/lib/cache.ts:17:21 - error TS2339: Property 'redisClient' does not exist on type 'RedisCache'.

17         return this.redisClient.sadd(LOCKED_STREAM_IDS_REDIS_KEY, streamIds);
                       ~~~~~~~~~~~
src/lib/cache.ts:20:21 - error TS2339: Property 'redisClient' does not exist on type 'RedisCache'.

20         return this.redisClient.srem(LOCKED_STREAM_IDS_REDIS_KEY, streamIds);
                       ~~~~~~~~~~~
src/lib/cache.ts:23:21 - error TS2339: Property 'redisClient' does not exist on type 'RedisCache'.

23         return this.redisClient.del(LOCKED_STREAM_IDS_REDIS_KEY);
                       ~~~~~~~~~~~
src/lib/cache.ts:26:28 - error TS2339: Property 'redisClient' does not exist on type 'RedisCache'.

26         return (await this.redisClient.smembers(NOT_FULL_STREAM_IDS_REDIS_KEY)) || [];
                              ~~~~~~~~~~~
src/lib/cache.ts:29:21 - error TS2339: Property 'redisClient' does not exist on type 'RedisCache'.

29         return this.redisClient.sadd(NOT_FULL_STREAM_IDS_REDIS_KEY, streamIds);
                       ~~~~~~~~~~~
src/lib/cache.ts:32:21 - error TS2339: Property 'redisClient' does not exist on type 'RedisCache'.

32         return this.redisClient.srem(NOT_FULL_STREAM_IDS_REDIS_KEY, streamIds);
                       ~~~~~~~~~~~
src/lib/cache.ts:35:21 - error TS2339: Property 'redisClient' does not exist on type 'RedisCache'.

35         return this.redisClient.del(NOT_FULL_STREAM_IDS_REDIS_KEY);
                       ~~~~~~~~~~~

It's very confusing because there is clearly exists redisClient property in RedisCache class.这很令人困惑,因为 RedisCache class 中显然存在 redisClient 属性。 I have tried everything: commenting libraries, comment line by line, changing settings in tsconfig, nothing helps.我已经尝试了一切:评论库,逐行评论,更改 tsconfig 中的设置,没有任何帮助。 Thanks for helping in advance:^)感谢您提前提供帮助:^)

Apparently, there can only be one export when using default keyword.显然,使用默认关键字时只能有一个导出。 https://stackoverflow.com/a/33307487/10092973 https://stackoverflow.com/a/33307487/10092973

Try removing default when exporting RedisCache, this could solve your issue!尝试在导出 RedisCache 时删除默认值,这可以解决您的问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM