gpt4 book ai didi

typescript - 如何在 TypeScript 接口(interface)中要求特定字符串

转载 作者:搜寻专家 更新时间:2023-10-30 20:28:43 24 4
gpt4 key购买 nike

我正在为第 3 方 js 库创建 TypeScript 定义文件。其中一种方法允许选项对象,并且选项对象的属性之一接受列表中的字符串:"collapse", "expand", "end-expand",和"none"

我有一个选项对象的接口(interface):

interface IOptions {
indent_size?: number;
indent_char?: string;
brace_style?: // "collapse" | "expand" | "end-expand" | "none"
}

接口(interface)是否可以强制执行此操作,因此如果您包含一个具有 brace_style 属性的 IOptions 对象,它将只允许可接受列表中的字符串?

最佳答案

这在 1.8 版中作为“字符串文字类型”发布

What's New in Typescript - String Literal Types

页面示例:

interface AnimationOptions {
deltaX: number;
deltaY: number;
easing: "ease-in" | "ease-out" | "ease-in-out";
}

关于typescript - 如何在 TypeScript 接口(interface)中要求特定字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26855423/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com