【发布时间】:2021-08-06 00:34:05
【问题描述】:
我有一个Client 类,用于存储应用程序需要保存在内存中的其他对象的缓存。对象缓存的结构是开发人员定义的。例如,如果我们有 Example 对象的缓存:
class Example {
property1: string;
property2: string;
}
开发者可能只想缓存property1。
import { EventEmitter } from "events";
// Constructor options for `Client`
interface ClientData {
cacheStrategies?: CacheStrategies;
}
// How various objects should be cached
interface CacheStrategies {
example?: ExampleCacheStrategies;
...
}
// Metadata for how each type of object should be cached
interface ExampleCacheStrategies {
cacheFor?: number;
customCache?: ExampleCustomCacheData;
}
// The custom structure of what parts of `example` objects should be cached
interface ExampleCustomCacheData {
property1?: boolean;
property2?: boolean;
}
// The object stored in `Client.exampleCache`, based on the custom structure defined in `ExampleCustomCacheData`
interface CustomExampleData<CachedExampleProperties extends ExampleCustomCacheData> {
property1: CachedExampleProperties["property1"] extends true ? string /* 1 */ : undefined;
property2: CachedExampleProperties["property2"] extends true ? string : undefined;
}
class Client<ClientOptions extends ClientData> extends EventEmitter {
// The map's value should be based on the custom structure defined in `ExampleCustomCacheData`
exampleCache: Map<string, CustomExampleData<ClientOptions["cacheStrategies"]["example"]["customCache"]>>;
constructor(clientData: ClientOptions) {
super();
this.exampleCache = new Map();
}
}
const client = new Client({
cacheStrategies: {
example: {
/**
* The properties of `example` objects that should be cached
* This says that `property1` should be cached (string (1))
*/
customCache: {
property1: true, // (2)
... // (3)
}
}
}
});
client.exampleCache.set("123", {
property1: "value"
});
const exampleObject = client.exampleCache.get("123");
if (exampleObject) {
// Should be `string` instead of `string | undefined` (2)
console.log(exampleObject.property1);
// `string | undefined`, as expected since it's falsey (3)
console.log(exampleObject.property2);
}
正如console.log()s 上方的 cmets 中所解释的,目标是从缓存中拉出的对象使 property1 成为 string 而不是 string | undefined。
问题是exampleCache: Map<string, CustomExampleData<ClientOptions["cacheStrategies"]["example"]["customCache"]>>; 不起作用,因为ClientOptions["cacheStrategies"] 和ClientOptions["cacheStrategies"]["example"] 都是可选的。以下也不起作用:
exampleCache: Map<string, CustomExampleData<ClientOptions["cacheStrategies"]?.["example"]?.["customCache"]>>;
'>' expected 在?. 出现错误。我该如何解决这个问题?
【问题讨论】:
-
这里的
"bans"是什么?请考虑修改此问题中的代码以构成minimal reproducible example,将其放入像The TypeScript Playground (link) 这样的独立IDE 时,清楚地展示了您面临的问题(并且只有那个问题)。这将使那些想要帮助您的人立即着手解决问题,而无需首先重新创建它。它将使您得到的任何答案都可以针对定义明确的用例进行测试。 -
@jcalz 我的错,发帖前忘记改了
标签: javascript typescript types type-parameter