You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用ReSharper 2017.3遇TypeScript返回类型不相关重载错误求解

Hey, let's tackle that ReSharper error you're seeing with your TypeScript overloads. The "Return types are not relatable" message is pointing out a mismatch between your overload signatures and the actual implementation's return behavior. Here's what's going wrong and how to fix it:

What's Causing the Error

Your first overload getCache<T>(key: string): T promises it will always return a value of type T when called without a default. But in reality, if the key doesn't exist in localStorage, your implementation returns undefined (since defaultValue is optional and not provided). ReSharper's strict type checking catches this mismatch—undefined isn't guaranteed to be part of type T, so the overload signature doesn't align with the actual return type.

Fix 1: Update the First Overload to Reflect Undefined

This is the most straightforward and logically accurate fix. Adjust the first overload's return type to T | undefined to match what the implementation actually returns:

public getCache<T>(key: string): T | undefined;
public getCache<T>(key: string, defaultValue: T): T;
public getCache<T>(key: string, defaultValue?: T): T | undefined {
  const result = localStorage.getItem(key);
  return result ? JSON.parse(result) as T : defaultValue;
}
  • The first overload now correctly signals that calling without a default might return undefined.
  • The second overload still guarantees a T type, since the default value ensures you'll never get undefined in that case. ReSharper recognizes this compatibility with the implementation's return type.

Fix 2: Use a Generic Default for Undefined

If you prefer to keep the first overload's return type as T (while allowing T to include undefined), you can set a default generic type:

public getCache<T = undefined>(key: string): T;
public getCache<T>(key: string, defaultValue: T): T;
public getCache<T = undefined>(key: string, defaultValue?: T): T {
  const result = localStorage.getItem(key);
  return (result ? JSON.parse(result) as T : defaultValue) as T;
}

Here, the default T = undefined means when you call getCache('myKey') without a type parameter, it returns undefined. When you specify a type like getCache<string>('myKey'), it returns string | undefined (since T is string, but the implementation might return undefined). The type assertion at the end helps ReSharper align the types, though this is slightly less explicit than Fix 1.

Fix 3: Split Into Separate Functions (Avoid Overloads Altogether)

If overloads are causing more trouble than they're worth, split the logic into two distinct functions. This makes the type behavior crystal clear:

public getCache<T>(key: string): T | undefined {
  const result = localStorage.getItem(key);
  return result ? JSON.parse(result) as T : undefined;
}

public getCacheWithDefault<T>(key: string, defaultValue: T): T {
  const cachedValue = this.getCache<T>(key);
  return cachedValue !== undefined ? cachedValue : defaultValue;
}

No more overload confusion—each function does exactly what its name suggests, and ReSharper won't flag any type mismatches.


内容的提问来源于stack exchange,提问作者Makla

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:36:37