简体   繁体   English

打字稿不警告可能未定义的数组?

[英]typescript don't warn about a potentially undefined array?

I have a question about how could I leverage typescript to warn about an array that could cause a run time error ?我有一个关于如何利用打字稿警告可能导致运行时错误的数组的问题? I have the following code I usually use a question mark after the array .. but should typescript know that this might cause a runtime error ?我有以下代码我通常在数组后使用问号..但是打字稿应该知道这可能会导致运行时错误吗?

 // cause run time erorr.. context.arr[props.id].includes(select) // no run time erorr context.arr[props.id]?.includes(select)

?. is optional chaining , which will properly chain with what follows if the expression on the left-hand side is defined (not undefined/null), and will evaluate to undefined otherwise:optional chaining如果左侧的表达式已定义(不是 undefined/null),它将与后面的内容正确链接,否则将评估为undefined

context.arr[props.id]?.includes(select)

// if context.arr[props.id] is undefined/null, equivalent to:
undefined
// (does not throw an error)


// if context.arr[props.id] is  notundefined/null, equivalent to:
context.arr[props.id].includes(select)

So, as long as TypeScript can see that, when context.arr[props.id] isn't undefined/null, it'll be an array, that statement is perfectly type-safe.因此,只要 TypeScript 可以看到,当context.arr[props.id]不是 undefined/null 时,它将是一个数组,该语句是完全类型安全的。 (though, TypeScript has problems with .includes , but that's another issue) (虽然,TypeScript.includes问题,但这是另一个问题)

You can enable the desired behavior by turning on Checked Indexed Accesses (available since 4.1 version).您可以通过打开Checked Indexed Accesses (自 4.1 版本起可用)来启用所需的行为。 Under this mode, indexed access is considered potentially undefined.在这种模式下,索引访问被认为可能是未定义的。 To enable it - set noUncheckedIndexedAccess compiler option to true .要启用它 - 将noUncheckedIndexedAccess 编译器选项设置为true

const arr = ['foo', 'bar']

// Expect error: Object is possibly 'undefined'.
arr[1].includes('test')

// OK
arr[1]?.includes('test')

Playground 操场


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 typescript 不警告这个 function 中的返回类型? - Why doesn't typescript warn about the return type in this function? 数组映射方法不能接受潜在的未定义参数 - Array map method couldn't accept potentially undefined parameter TypeScript 中的索引签名不处理未定义的 - Index signatures in TypeScript don't handle undefined 当我尝试访问可能未定义的数组上的属性时,如何使 TypeScript 出错? - How can I make TypeScript error out when I try to access a property on a potentially undefined array? Typescript-在运行时检查类型并警告错误 - Typescript - check type in runtime and warn about errors 我不知道打字稿中的返回类型 - I don't know about return type in typescript 如果数组索引可能不可用,则获取 TypeScript 发出警告 - Getting TypeScript to warn if array index may not be available 使用 typescript 定义文件 (.d.ts) 键入 javascript 代码不会警告原始类型错误 - using typescript definition file (.d.ts) for typing javascript code doesn't warn about primitive type errors 使用中的数据请求 function 可能为空/未定义 - Typescript,反应 - Data in useRequest function is potentially null/undefined - Typescript, React Typescript 有没有办法警告将对象分配给另一个对象作为引用分配 - Is there a way for Typescript to warn about object assignment into another object as a reference assignment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM