- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
给定一个接受单个 string[]
的函数参数myArray
.
如果我评估 .length
属性并且该属性大于 0,那么(假设我的变量不是 any
变相)它不可能用于 myArray[0]
未定义。但是,如果我启用 noUncheckedIndexedAccess
,其类型将为 string | undefined
const myFunc = (myArray: string[]) => {
if(myArray.length > 0) {
const foo = myArray[0] // now typed at 'string | undefined'
}
}
现在我可以更改 if 语句来评估
myArray[0]
, 和
undefined
如您所料,已从类型中删除。但是,如果我现在想检查数组的长度是否大于 6,该怎么办。我不想对索引 0-5 执行相同的操作以正确缩小类型。例如:
const myFunc = (myArray: string[]) => {
if(myArray[0] && myArray[1] && myArray[2] && myArray[3] && myArray[4] && myArray[5]) {
const foo = myArray[0] // now typed at 'string', but this is uggggly
}
}
有没有更优雅的方法来根据数组的长度缩小类型,还是我将不得不考虑为 TypeScript 代码库做出贡献?
最佳答案
正如您所怀疑的那样,TypeScript 不会根据对其 length
的检查自动缩小数组的类型。属性(property)。之前已在 microsoft/TypeScript#38000 中提出了这一建议。 ,标记为“太复杂”。看起来之前有人建议过,在 microsoft/TypeScript#28837 ,仍处于打开状态并标记为“等待更多反馈”。也许你可以去那个问题并留下反馈,说明为什么这样的事情对你有帮助,为什么当前的解决方案不够充分,但我不知道它会产生多大影响。无论哪种方式,我都怀疑 TS 团队现在是否正在接受拉取请求来实现这样的功能。
如果没有任何自动缩小,你可以写一个 user defined type guard function这有你想要的效果。这是一种可能的实现:
type Indices<L extends number, T extends number[] = []> =
T['length'] extends L ? T[number] : Indices<L, [T['length'], ...T]>;
type LengthAtLeast<T extends readonly any[], L extends number> =
Pick<Required<T>, Indices<L>>
function hasLengthAtLeast<T extends readonly any[], L extends number>(
arr: T, len: L
): arr is T & LengthAtLeast<T, L> {
return arr.length >= len;
}
Indices<L>
type 旨在采用单个的、相对较小的、非负的整数数字
literal type
L
并返回
union长度为
L
的数组的数字索引.另一种说法是
Indices<L>
应该是小于
L
的非负整数的并集.观察:
type ZeroToNine = Indices<10>
// type ZeroToNine = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
这种类型的工作原理是利用
recursive conditional types连同
variadic tuple types步行从
0
高达
L
.递归类型往往能正常工作,直到它们不能正常工作,这里的一个重要警告是,如果您传入
L
,事情会很奇怪,甚至会引发错误。太大,或小数,或负数,或
number
,或工会。这是这种方法的一个重要警告。
LengthAtLeast<T, L>
采用数组类型
T
和长度
L
,并返回一个对象,该对象已知在长度至少为
L
的数组的所有索引处都具有属性。 .像这样:
type Test = LengthAtLeast<["a"?, "b"?, "c"?, "d"?, "e"?], 3>
/* type Test = {
0: "a";
1: "b";
2: "c";
} */
type Test2 = LengthAtLeast<string[], 2>
/* type Test2 = {
0: string;
1: string;
} */
最后,
hasLengthAtLeast(arr, len)
是类型保护函数。如果返回
true
,然后
arr
从类型
T
缩小至
T & LengthAtLeast<T, L>
.让我们看看它的实际效果:
const myFunc = (myArray: string[]) => {
if (hasLengthAtLeast(myArray, 6)) {
myArray[0].toUpperCase(); // okay
myArray[5].toUpperCase(); // okay
myArray[6].toUpperCase(); // error, possibly undefined
}
}
看起来挺好的。编译器很高兴允许您处理
myArray[0]
和
myArray[5]
如定义,但
myArray[6]
仍然可能未定义。
null
assertion operator 可能是值得的。喜欢
myArray[0]!.toUpperCase()
而不必担心让编译器为您验证类型安全。
len
的值,那么您可能不想要脆弱的递归条件类型,而是构建更健壮但不那么灵活的东西(例如可能只适用于特定的
len
值,如
in a comment on microsoft/TypeScript#38000 的重载类型保护)。
关于typescript - 当 noUncheckedIndexedAccess 为 true 时,按长度缩小数组类型的类型安全方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69368851/
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
我正在尝试创建一个可以像在 Excel 中一样放大和缩小的 QTableView。 此处提出了类似的问题:Zooming function on a QWidget 但是,我在 PyQt 而不是 C
如图所示。 我在 QScrollArea 中有 QWidget。QWidget 充当细胞图像和一些基于矢量的轮廓数据的渲染小部件。用户可以执行放大/缩小操作,简单地发生的是,它改变了 QPaint
双击 MKMapView 时:放大。 但是如何缩小呢? 最佳答案 总是使用两根手指来放大和缩小。在模拟器上,您需要按住选项键才能在模拟屏幕上显示“两根手指”(我认为这是 Alt 键,但我在 Mac 上
我有一些 javascript for {} 循环,我在整个项目中重复使用它们,它们都类似于: for (var i = 0; i < things.length; i++) { consol
我知道我可以使用C-x C- +进行放大/缩小,但这仅适用于当前文件。一旦我打开另一个文本,文本将恢复为默认值,一遍又一遍地做起来真的很累。如何保持当前emacs session 的全局缩放级别? 如
我对使用编译器工具自动化/简化 Angular 项目感兴趣,这可能适用于其他所有事情,但 Angular 注入(inject)和命名空间很尴尬,足以逃避编译器知识。执行此操作的最佳/专业方法是什么?
有没有办法在emacs上放大和缩小(动态改变字体大小,相当流畅)? 最佳答案 尝试 C-x C-+ 和 C-x C--;即 Control-x Control-减号/Control-再加上。 在一个组
我有一个Windows表单对象,其中包含3个对象,树 View ,richtextbox和tabcontrol。它们没有停靠在Windows窗体中,而是被 anchor 定(顶部+左侧)。 我已经编写
我想向 javascript-mode 添加功能,以便每当我在当前缓冲区上保存 Javascript 文件时,它都会在使用相对路径定义的目录中创建该文件的缩小文件,例如 ../foo 具有相同的文件名
这里有一些关于缩小.war文件的教程,甚至一些帖子。但是,最常见的技术(在Config.groovy中包含grails.war.resources = {})似乎对我不起作用。无论如何,grails会
如何使用 ScaleTransition缩小图像?我现在有这个,它只能放大。如果我误解了该方法,我不会,但我将其从 1 缩放到 0.8。由于某种原因,这种情况仍在扩大。 ScaleTransition
基本上,我想问ReplicaSets是否与CronJobs的suspend: "true"选项类似,但我愿意接受其他建议。 最佳答案 From the official Kubernetes doc
我想使用 boost::polygon 扩展/收缩带孔的多边形。所以澄清一点,我有一个单一的数据结构 boost::polygon::polygon_with_holes_data inPoly 其中
我有一个 map 列表: [%{~D[2019-02-11] => 7}, %{~D[2019-02-12] => 1}, %{~D[2019-02-15] => 1}] 我正在尝试将其变成一张大
我正在制作一个横幅,您可以使用jquery幻灯片功能缩放图像并且可以拖动图像。 除了一件事之外,它工作完美。当您使用图像下方的幻灯片放大图像时,效果非常好。您可以将图像拖动到您想要的位置。但当你想用幻
我们有一个 extjs 应用程序,其中我们布置的结构与 Sencha 推荐的结构不完全匹配。在我们的结构中,我们没有 app.js,但我们有一个 js,其中我们提到了自动加载和启动功能,示例如下以及文
我想在 Chrome/Firefox 中运行的应用程序是: 用 typescript 写 使用 React 使用 es 下一个功能(模块、导入等)编写 有一些导入是纯 js 文件 网页包 3 我可以在
我正在尝试像此处一样应用 Google map 的放大/缩小 - https://www.google.com/maps/@36.241201,-98.1261798,5.13z?hl=en我无法让它
我正在使用 Protractor ,需要缩小到 50%,我尝试了 StackOverflow 上发布的其他几个问题的解决方案,但没有任何效果。其中一些包括: browser.actions().key
我是一名优秀的程序员,十分优秀!