- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我能想到的最简单的例子:
interface text {
type: "text";
text: string;
}
interface integer {
type: "integer";
integer: number;
}
type Config = text | integer;
function appendToBody(config: Config) {
if (!config[config.type]) return;
document.body.append(config[config.type]);
}
function createConfig(type: "text" | "integer", value: string | number) {
// how to let TS know that type and [type] will be matching?
// TS naturally assumes { type: 'text', text: 5 } is possible, even though it isn't
const config: Config = {
type,
[type]: value
};
appendToBody(config);
}
createConfig("text", "hello world");
https://codesandbox.io/s/6gtq8
基本上我使用的模式是通过询问 obj[obj.type] 来提取值。这对我的实际情况很有用,因为我可以制作一个通用的解析器,根据类型提取我需要的值。它还具有如果类型更改时不必清空的优点,因为它将保存在不同的 [type] 上,并且如果您改回原值,则不会丢失旧值。
我只是想不通如何让 typescript 理解 type 和 [type] 的所有可能组合都包含在“Config”类型中。
最佳答案
首先,我们要明确integer
应该有一个名为integer
的键,而不是number
,对吧?像这样:
interface text {
type: "text";
text: string;
}
interface integer {
type: "integer";
integer: number; // right?
}
type Config = text | integer;
function appendToBody(config: Config) {
if (!config[config.type]) return;
document.body.append(config[config.type]);
}
好的。
createConfig()
有两个类型安全问题...一个是为调用者 强制类型安全,另一个是在 中强制类型安全em>实现。现在,编译器在实现内部警告您它无法验证 { type: type, [type]: value }
是有效的 Config
。现在警告您是正确的,因为调用者可以毫无错误地执行以下操作:
createConfig("text", 5); // no error, oops!
没有简单的方法可以为调用者和实现者解决这个问题。每一方都有自己的问题。
要为调用者修复它,您可以使用 overloads像下面这样:
function createConfig(type: "text", value: string): void;
function createConfig(type: "integer", value: number): void;
function createConfig(type: string, value: any): void {
// impl
}
这很容易理解,但需要为 Config
联合的每个组成部分添加一个重载。您也可以使用 conditional类型和 generic函数,如下:
type Lookup<T, K> = K extends keyof T ? T[K] : never;
type ConfigFor<T extends Config['type']> = Extract<Config, { type: T }>;
function createConfig<T extends Config['type']>(
type: T,
value: Lookup<ConfigFor<T>, T>
) {
// impl
}
这很复杂,但假设 Config
联合的每个元素 C
都符合 type
属性命名的约束,它会自动正确运行值属性的相关键。
这两种情况都会导致调用者出现以下行为:
createConfig("text", 5); // error
createConfig("integer", 5); // okay
createConfig("text", "hello world"); // okay
createConfig("integer", "hello world"); // error
要为实现修复它,(这是你的实际问题),编译器仍然无法弄清楚 config
是一个有效的 Config
即使固定调用签名。对于重载,是因为实现签名过于松散,无法表达约束,重载实现do not currently do any control flow analysis based on the call signatures .对于泛型条件类型,这是因为依赖于其中未解析的泛型类型参数的条件类型也是do not get narrowed via control flow analysis .因此,在这两种情况下,编译器基本上都放弃了在实现中强制执行相关数据类型的类型安全。我经常wished对于某种允许您提示编译器通过控制流分析遍历联合类型的机制,但现在这只是一个幻想。
那么,你能做什么?据我所知,这里实际上只有两种前进方式。您要么使用 type assertion : 保持你的代码不变,但只告诉编译器你将负责确保类型安全,如:
const configAsserted = {
type,
[type]: value
} as any as Config;
appendToBody(configAsserted);
或者,您在运行时进行额外的手动检查,让编译器相信您所做的是安全的,如:
let configManual: Config;
if (type === "integer" && typeof value === "number") {
configManual = { type: "integer", integer: value };
} else if (type === "text" && typeof value === "string") {
configManual = { type: "text", text: value };
} else {
throw new Error("YOU MESSED UP");
}
appendToBody(configManual);
两种方式都有效。当您将成分添加到 Config
时,断言不太安全,但可扩展性更好。手动检查是安全的,但它是多余的,每次向 Config
添加成分时都必须添加代码。
因此,在我看来,您有多种选择。就我个人而言,我会选择扩展性更好的解决方案,如下所示:
type Lookup<T, K> = K extends keyof T ? T[K] : never;
type ConfigFor<T extends Config['type']> = Extract<Config, { type: T }>;
function createConfig<T extends Config['type']>(
type: T,
value: Lookup<ConfigFor<T>, T>
) {
const config = {
type,
[type]: value
} as any as Config;
appendToBody(config);
}
好的,希望对你有帮助。祝你好运!
关于javascript - 如何告诉 typescript {类型:enum, [类型: string]:value} are in my type?的所有可能组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56144368/
我不明白为什么这甚至可以编译。我尝试过不同的格式,它们似乎都有效。 为什么有一个 enum of enum of enum of.. 是合法的? interface I { enum E im
我有大型数据库(数百万行),我正在尝试为 2 个字段的数据类型做出最佳选择。我制作的大部分内容都是 varchar 或 INT。但是,我想知道 Enum 是否是最好的方法的 2 个字段。 字段 1第一
我是 C++ 新手,我想知道是否有人可以帮助我理解原因 enum difficulty { NOVICE, EASY, NORMAL, HARD, UNBEATABLE }; difficulty m
从 native 代码强制转换(在 C++/CLI 中)的正确方法是什么 enum到托管代码enum其中包含相同的 enum值(value)观?与使用 C# 的强制转换方式有什么区别,例如 (int)
我有以下枚举 [Flags] public enum WeekDays { Monday = 1, Tuesday = 2, Wednesday = 4, Thursd
我刚刚学习 Java 中的枚举。当我运行下面的代码时,我收到一个错误,我也在下面重现该错误。基本上,我的问题是:当我在枚举中定义一个方法,并且在该方法中我想检查枚举的值以便我可以根据该值执行某些操作时
我想要一些语法糖来打开 Enum .当然,一个if else块按预期工作: @enum Fruit apple=1 orange=2 kiwi=3 function talk1(fruit::Frui
我试图在 Enum.each 的函数内为变量设置一个值,但在循环结束时,变量为空,我不知道为什么会出现这种行为。 代码: base = "master" candidates = ["stream",
This question already has answers here: Is there a way to use existing structs as enum variants? (2个
我想让 Java 中的枚举器具有其他枚举作为属性。 public enum Direction { Up(Down), Down(Up), Left(Right), R
我有一个包含两种不同可能“类型”的枚举和一个可以返回其中任何一种的函数,封装在 Option 中: enum Possibilities { First(i32), Second(St
我刚刚注意到 pyhton 中 Enum+defaultdict 的一个非常奇怪的行为。我定义了一个这样的枚举,它收集了一些默认词典: from enum import Enum from colle
我想在运行时从配置文件生成一些类型。为简单起见,假设我已经将数据加载为 Python 字典: color_values = dict(RED = 1, YELLOW = 2, GREEN = 3) 我
我想创建一个方法,在传入参数的任何枚举类上实现 valueOf(并包装一些专门的错误/缺失名称代码)。所以基本上,我有几个枚举,例如: enum Enum1{ A, B, C } enum Enum2
我有一个user数据库表: CREATE TABLE IF NOT EXISTS `user` ( `user_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
这是我的 JPA @Repository,在这里我们可以得到 list与 findByFullNameContaining(String query) - 通过在查询中提供 fullName 的子字符
(也在这里打开了一个问题:https://github.com/square/moshi/issues/768 但被要求打开一个stackoverflow问题)我正在编写一个通用适配器来转换带有枚举值
请帮助找到以下情况的最佳方法: 有一个表,大约有 20 列。 每列都有自己的短名称、全名称和类型(数字或字符串)。 每个列类型都可以有自己的运算符 - 例如,字符串 - 包含、等于;数字 - 更多、更
如果我在 python 中按功能创建了 enum.Enum(本例中为 3.7),如何从中检索类的名称? import enum def print_the_enum_class_name(some_e
我正在编写一个与第 3 方程序交互的程序。这个第 3 方程序允许用户制作可以运行在第 3 方程序中进行的步骤记录的按钮。 但! 这些按钮还可以运行用户定义的批处理文件。因此,我使用此功能通过创建文件并
我是一名优秀的程序员,十分优秀!