简体   繁体   English

当您将返回类型方法的 output 分配给变量时,为什么 Typescript 无法推断类型?

[英]Why Typescript can't infer type when you assign a return typed method's output to a variable?

I have a reusable service so I have created a public API with documentation and types to ease the usage of clients.我有一个可重用的服务,所以我创建了一个带有文档和类型的公共 API 来简化客户端的使用。

interface Storable {
  setItem(key: string, value: string): any;
  getItem(key: string): string;
  removeItem(key: string): any;
}

@Injectable({
  providedIn: 'root'
})
export class DataStorageService {
   private expirableSecureLocalStorage:any;
   private secureLocalStorage:any;
   private expirableLocalStorage:any;
   constructor(/*...*/) {
    this.expirableSecureLocalStorage = this.createExpirableStorage(this.createSecureStorage(localStorage));
    this.secureLocalStorage = this.createSecureStorage(localStorage);
    this.expirableLocalStorage = this.createExpirableStorage(localStorage);
   }

   /**
   * Returns a handle to localStorage: Use when you want to compose/decorate storages.
   */
  getLocalStore(): Storable {
    return localStorage;
  }

  /**
   * Returns a handle to sesionStorage: Use when you want to compose/decorate storages.
   */
  getSessionStore(): Storable {
    return sessionStorage;
  }
  /** 
   * Recommended: Singleton - prefer for ordinary operations
   */
  getExpirableSecureLocalStorage(): Storable {
    return this.expirableSecureLocalStorage;
  }

  /** 
   * Recommended: Singleton - prefer for ordinary operations
   */
  getSecureLocalStorage(): Storable {
    return this.secureLocalStorage;
  }

  /** 
   * Recommended: Singleton - prefer for ordinary operations
   */
  getExpirableLocalStorage(): Storable {
    return this.expirableLocalStorage;
  }

  //...

}

Then in a client:然后在客户端:

@Injectable({
  providedIn: 'root'
})
export class FeatureService {

  expirableSecureLocalStorage:any;

  constructor(private apiService: ApiService, private dataStorageService: DataStorageService) {  
    this.expirableSecureLocalStorage = dataStorageService.getExpirableSecureLocalStorage();                                 
  }
 
  async getFeatures(keyname: string) {
    let features: any;
    let feature: any;
    try {
      let featuresLocalData = this.expirableSecureLocalStorage.getItem("features");
      //...
    }
    //...
   }

When this code has evolved to current status, I have realized that when I have added the type Storable to DataStorageService , vscode's autocomplete/intellisense has started to suggest methods.当这段代码发展到现在的状态时,我意识到当我将Storable类型添加到DataStorageService时,vscode 的自动完成/智能感知已经开始建议方法。 However, in the client when I keep the method's dataStorageService.getExpirableSecureLocalStorage() which returns a Storable result in a reference variable expirableSecureLocalStorage:any and when I try to use a method of it like getItem as this.expirableSecureLocalStorage.getItem("features") , vscode does not offer getItem and other two methods directly.但是,在客户端中,当我保留方法的dataStorageService.getExpirableSecureLocalStorage()时,它在参考变量expirableSecureLocalStorage:any中返回一个Storable的结果,当我尝试使用它的方法时,比如getItem as this.expirableSecureLocalStorage.getItem("features") ,vscode没有直接提供getItem等两个方法。

Why can't typescript infer the type of the reference variable which is assigned to the result of the method which has a return type?为什么 typescript 不能推断分配给具有返回类型的方法的结果的引用变量的类型?

What should I have to do to make vscode suggest methods that is available?我应该怎么做才能使 vscode 建议方法可用?

Your code expirableSecureLocalStorage:any;你的代码expirableSecureLocalStorage:any; tells typescript that whatever you put in expirableSecureLocalStorage , it should handle it as "anything", effectively removing its type.告诉 typescript 无论你在expirableSecureLocalStorage中放入什么,它都应该将其处理为“任何东西”,有效地删除它的类型。

You shoud export your Storable interface and declare expirableSecureLocalStorage like this:你应该导出你的Storable接口并像这样声明expirableSecureLocalStorage

expirableSecureLocalStorage: Storable;

Why can't typescript infer the type of the reference variable which is assigned to the result of the method which has a return type?为什么 typescript 不能推断分配给具有返回类型的方法的结果的引用变量的类型?

Because you have explicitly told it to not infer a type by explicitly giving the type yourself .因为您已经明确告诉它不要通过自己显式给出类型来推断类型。

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

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