- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
type Word = "foo" | "bar" | "baz";
const schema = {
foo: ["foo"] as const,
bar: ["bar"] as const,
baX: ["bar", "baz"] as const,
};
const testFn = (schemaKey: keyof typeof schema, word: Word) => {
const array = schema[schemaKey]
array.includes(word);
// ^
// TS2345: Argument of type 'string' is not assignable to parameter of type 'never'.
// Type 'string' is not assignable to type 'never'
}
Playground link
最佳答案
让我们来看看其中的一些类型。我们有
array: readonly ["foo"] | readonly ["bar"] | readonly ["bar", "baz"]
现在,
include
上的
T[]
方法采用
T
类型的参数,即列表的正确类型的参数。这意味着,在 Typescript 中,询问字符串是否是整数数组的元素并不是很好的类型,因为这种代码很可能一开始就是错误的。
["foo"]
有一个方法
include
这需要一个
"foo"
作为参数(即,其唯一的居民的类型是文本字符串
"foo"
。同样,
["bar"]
有一个方法
include
这需要一个
"bar"
作为参数,并
["bar", "baz"]
具有
include
这需要的元件类型
"bar" | "baz"
作为参数。
include
必须采用对所有这三个都有效的东西。也就是说,我们的
include
必须接受一个类型为
"foo" & "bar" & "baz"
所以我们可以传递与上述所有三个字符串相等的任何字符串。我不知道任何这样的字符串,Typescript 编译器也不知道,所以它正确地说该类型是无人居住的,即
never
。没有类型安全的方法来调用
include
。
any
-cast 你的方式绕过它,就像你可以在 Typescript 中的所有东西一样。但更聪明的技术是保持类型安全。看,Typescript 推断的类型对于您的
array
变量来说太具体了,这是您开始处理文字类型时的常见问题。但似乎你想要的类型是
array : readonly Word[]
碰巧,它是我在本文顶部指出的类型的完全有效的父类(super class)型。我们可以简单地使用显式类型声明来使代码工作
const array: readonly Word[] = schema[schemaKey]
请注意,这不是类型转换。我们不会告诉类型系统“我比你更了解,嘘”。
readonly Word[]
是 Typescript 推断的字面联合的完全有效的父类(super class)型。这段代码仍然经过类型检查和类型安全。我们只需要给编译器一些关于我们意图的提示。
关于typescript - 为什么元组联合期望 `never` 作为 `.includes()` 参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68106505/
我在远程机器上导航基于 Java 的 CLI 菜单,并在 bash 脚本中使用 expect,我试图从输出中提取某些内容而不离开 expect session 。 我的脚本中的 Expect 命令是:
我正在尝试使用 expect.h header 编译用 c 编写的程序。我正在尝试这个: cc -I/usr/include main.c -lexpect -ltcl cc -I/usr/inclu
我正在使用Expect与SSH session 和ERP程序进行自动交互。 而不是依靠正则表达式来捕获我期望脚本中的变量,是否有可能在收到用户的特定击键后将屏幕区域(例如一个字段)捕获为代码中的变量?
我是 PHP 面向对象编程的新手。我有个问题。我写了一个代码,但它不起作用。我知道这很容易,但我想知道它有什么问题。我出现以下错误: 当我尝试在另一个文件中使用它时,我现在遇到了这个错误:( 最佳答
声明了哪些出现了前所未见的错误,并试图找到解决方案。与以前的程序一样奇怪,它使用相同的语法但不会抛出任何错误 这是一个使用游标从表中检索信息,然后将其插入到另一个表中的过程,这样做是为了可以使用其中的
我已经用 CASE 编写了一个查询,但遇到了 () 问题。 select SM.subscriber_name as name , SM.accountType as accountTy
这个问题在这里已经有了答案: Why does removing return give me an error: expected type `()` but found type (1 个回答)
我有一个脚本可以登录服务器并执行一些命令。我需要能够从每个命令中检索返回代码,以确定脚本是否成功。我写了以下脚本,但没有按照我的意愿进行。目标是执行“cd/I/dont/exist”,这会产生错误代码
关闭。 这个问题需要 debugging details 。它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and
我正在运行一个 expect 脚本,它将生成一些来自 stdin 的动态输入。 是否有一种方法/模式可以解决从标准输入读取并将相关输入存储(?)到某处以在后面的步骤中处理/解析的概念? 示例: ./m
我正在运行一个 expect 脚本,该脚本在远程机器上调用多个脚本。这些 shell 脚本返回颜色输出(主要是红色和绿色)。问题是,那些颜色代码进入了我不想要的 log_file 和 STDOUT。我
我正在开发一个脚本,用于对软件安装进行回归测试。期望代码如下。前几行代码在浏览并同意许可证文件的地方运行良好。但是,脚本在“请输入有效许可证文件的路径名:”处停止,并且不执行任何操作。 (注意:手动安
我们创建以下简单的 expect 脚本以运行 netdata-installer.sh 预期脚本是: #!/usr/bin/expect set timeout 20 send "cd /tmp/ne
有人有T_PAAMAYIM_NEKUDOTAYIM吗? 最佳答案 是双冒号运算符 :: (见 list of parser tokens)。 关于PHP 期望 T_PAAMAYIM_NEKUDOTAY
我正在使用 Vercel SWR Hook usrSWR,我希望我可以将数据存储在某个遥远组件的缓存中,而不必使用上下文或其他一些全局状态管理器。 具体来说,我在 IndexPage 中使用 init
我刚刚注意到,如果我添加 if,Spock 不会断言条件。预期块中的子句,如 def myTest() { given: a = true expect: if ( a ) {
我有一个这样的方法: getValues(...args: Array) : Array { return args.map(k => { return this.shared
我正在使用 typescript + jest,并且在创建模拟实现时遇到了一些类型检查问题。例如,我想模拟 Credentials来自 aws-sdk 的对象: import { Credential
我依赖于一个以 Map 作为参数的方法。 public interface Service { void doSomething(Map map); } 我想写一个断言,用适当的 map 内容
我有一个适配器,它有一个方法,它采用可变参数列表,并将其转发给一个在我使用的框架中采用相同参数的方法。我想测试我的适配器是否正确转发了参数。然而,我不希望我的测试知道框架支持哪种参数。 我有一个工作期
我是一名优秀的程序员,十分优秀!