简体   繁体   English

用对象的键作为字符串文字表示表示元组类型的类型

[英]Type representing a tuple type with the keys of an object as string literals

Given an interface: 给定一个接口:

interface EPostageInsertExEvent_Parameter {
    readonly Doc: Word.Document;
    cpDeliveryAddrStart: number;
    cpDeliveryAddrEnd: number;
    readonly cpReturnAddrStart: number,
    readonly cpReturnAddrEnd: number;
    readonly xaWidth: number;
    readonly yaHeight: number;
    readonly bstrPrinterName: string;
    readonly bstrPaperFeed: string;
    readonly fPrint: boolean;
    fCancel: boolean;
}

how can I define a tuple type consisting of the keys of that interface as string literals? 如何定义由该接口的键组成的元组类型作为字符串文字? In other words, the equivalent of the following: 换句话说,相当于以下内容:

type EPostageInsertExEvent_ArgNames = ['Doc', 'cpDeliveryAddrStart', 'cpDeliveryAddrEnd',
    'cpReturnAddrStart', 'cpReturnAddrEnd', 'xaWidth', 'yaHeight', 'bstrPrinterName',
    'bstrPaperFeed', 'fPrint', 'fCancel'];

This is not currently possible in TypeScript. 当前在TypeScript中无法实现。 There is an feature request in GitHub which would allow this, but there hasn't been much movement there. GitHub中有一个功能请求可以允许这样做,但是那里并没有太多的动静。 (You might want to go and give it a 👍 and describe your use case, or you might not.) TypeScript doesn't yet have extensive support for type functions. (您可能想要给它加上👍来描述您的用例,或者您可能不需要。)TypeScript尚未对类型函数提供广泛的支持。 Right now the closest you can get is: 现在,您可以获得的最接近的是:

type EPostageInsertExEvent_ArgNames = Array<keyof EPostageInsertExEvent_Parameter>;

but that does not guarantee anything about the presence, uniqueness, or order of the keys. 但这不能保证键的存在,唯一性或顺序。 Sorry not to have better news. 抱歉,没有更好的消息。

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

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