簡體   English   中英

存在時類型上不存在屬性

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

我有這個 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)
  }
}

當我嘗試導入它時,出現以下錯誤:

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);
                       ~~~~~~~~~~~

這很令人困惑,因為 RedisCache class 中顯然存在 redisClient 屬性。 我已經嘗試了一切:評論庫,逐行評論,更改 tsconfig 中的設置,沒有任何幫助。 感謝您提前提供幫助:^)

顯然,使用默認關鍵字時只能有一個導出。 https://stackoverflow.com/a/33307487/10092973

嘗試在導出 RedisCache 時刪除默認值,這可以解決您的問題!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM