如何在 TypeScript 接口中要求特定字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26855423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to require a specific string in TypeScript interface
提问by thetallweeks
I'm creating a TypeScript definition file for a 3rd party js library. One of the methods allows for an options object, and one of the properties of the options object accepts a string from the list: "collapse"
, "expand"
, "end-expand"
, and "none"
.
我正在为 3rd 方 js 库创建一个 TypeScript 定义文件。一项所述的方法允许对选择对象,并且选项对象的属性中的一个接收来自列表的字符串:"collapse"
,"expand"
,"end-expand"
,和"none"
。
I have an interface for the options object:
我有一个选项对象的接口:
interface IOptions {
indent_size?: number;
indent_char?: string;
brace_style?: // "collapse" | "expand" | "end-expand" | "none"
}
Can the interface enforce this, so if you include an IOptions
object with the brace_style
property, it will only allow a string that is in the acceptable list?
接口是否可以强制执行此操作,因此如果您包含IOptions
具有该brace_style
属性的对象,它将只允许可接受列表中的字符串?
回答by Ryan Q
This was released in version 1.8 as "string literal types"
这是在 1.8 版中作为“字符串文字类型”发布的
What's New in Typescript - String Literal Types
Example from the page:
页面示例:
interface AnimationOptions {
deltaX: number;
deltaY: number;
easing: "ease-in" | "ease-out" | "ease-in-out";
}
回答by Denis Khay
Try this
尝试这个
export type ReadingTypes = 'some'|'variants'|'of'|'strings';
export interface IReadings {
param:ReadingTypes
}
回答by Kuba Jagoda
Maybe not exactly what you wanted, but Enum
s seem like a perfect solution for you.
也许不完全是您想要的,但Enum
s 似乎是您的完美解决方案。
enum BraceStyle {Collapse, Expand, EndExpand, None}
interface IOptions {
indent_size?: number;
indent_char?: string;
brace_style?: BraceStyle
}
Enums are, however, number-based. It means that during runtime a real value for e.g. BraceStyle.Collapse
will be 0 in this case. But you can use them with other, even non-typescript scripts, since they compile to objects. This is how BraceStyle
will look after compile&run:
然而,枚举是基于数字的。这意味着在运行期间,BraceStyle.Collapse
在这种情况下,例如的实际值将为 0。但是您可以将它们与其他甚至非打字稿脚本一起使用,因为它们编译为对象。这是BraceStyle
编译和运行后的样子:
{
0: "Collapse",
1: "Expand",
2: "EndExpand",
3: "None",
Collapse: 0,
Expand: 1,
EndExpand: 2,
None: 3
}
If you want strings instead, you can use a class with static members, as described here
如果您想要字符串,则可以使用具有静态成员的类,如here所述
回答by Willem van der Veen
TS offers a typing to specific string values, which are called String literal types.
TS 提供对特定字符串值的类型化,称为String 文字类型。
Here is an example of how to use them:
以下是如何使用它们的示例:
type style = "collapse" | "expand" | "end-expand" | "none";
interface IOptions {
indent_size?: number;
indent_char?: string;
brace_style1?: "collapse" | "expand" | "end-expand" | "none";
brace_style2?: style;
}
// Ok
let obj1: IOptions = {brace_style1: 'collapse'};
// Compile time error:
// Type '"collapsessss"' is not assignable to type '"collapse" | "expand" | "end-expand" | "none" | undefined'.
let obj2: IOptions = {brace_style1: 'collapsessss'};
回答by Sameh
function keysOf<T>(obj: T, key: keyof T) { return obj[key]; }
interface SomeInterface {
a: string;
}
const instance: SomeInterface = { a: 'some value'};
let value = keysOf<SomeInterface>(instance, 'b'); // invalid
value = keysOf<SomeInterface>(instance, 'a'); // valid