这与AWS SDK有关,它有几个客户端,每个客户端都有带有属性的命名空间.
这个库导出AWS
,它有几个客户端,例如DynamoDB
和ACM
.DynamoDB
有一个名为DocumentClient
的房产.ACM
有一个名为CertificateBody
的房产.
class _DocumentClient {}
namespace _DocumentClient {}
// DynamoDB that is imported in the AWS module (see below)
declare class _DynamoDB {}
declare namespace _DynamoDB {
export import DocumentClient = _DocumentClient;
}
// ACM client that is imported in the AWS module (see below)
declare class _ACM {}
declare namespace _ACM {
export type CertificateBody = string;
}
// AWS module (mimicking)
// The original library (as linked above) exports a lot more clients...
namespace AWS {
export import ACM = _ACM;
export import DynamoDB = _DynamoDB;
}
我试图创建一个类型,它产生一个像"DynamoDB.DocumentClient" | "ACM.CertificateBody"
这样的字符串字面量的联合.
我已经try 获取每个客户端的所有属性的列表,但这似乎是一项不可能完成的任务,更不用说使用点符号附加客户端和属性了.
我只得到了prototype
个字符串,而不是想要的DocumentClient
和CertificateBody
个字符串.
// this works - ("ADM" | "DynamoDB")
type ClientName = keyof typeof AWS;
// shouldn't this yield "DocumentClient" | "CertificateBody"? Using this type only means I can use "prototype" :/
type NestedClientName<C extends ClientName> = keyof typeof AWS[C];
// I want to be able to get a type with a union of string literals like `"ACM.CertificateBody" | "DynamoDB.DocumentClient"
type NestedClientFullName<C extends ClientName, NC extends NestedClientName<C>> = `${C}.${NC}`;
不幸的是,这NestedClientName
个(因此,NestedClientFullName
个)都没有产生公平的结果.
Here is a permalink to the full file on GitHub for context,以显示原始aws-sdk
文件.我已经在上面的两个片段和下面的操场上复制了它.