typescript - 如何访问可为空的 typescript 嵌套类型

标签 typescript types typeerror tslint typescript-eslint

我想提取一个嵌套类型,如下面的代码所示。有没有办法对可为空的字段执行此操作?

type Query = { a: { b?: { c: string } };

type c = Query['a']['b']['c'];

Property 'c' does not exist on type '{ c: string; } | undefined'.ts(2339)

最佳答案

type Query = { a: { b?: { c: string } } };

type foo = NonNullable<Query['a']['b']>['c'];

Playground

关于typescript - 如何访问可为空的 typescript 嵌套类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74204667/

相关文章:

types - Python:测试值是否可以在列表理解中转换为 int

javascript - 从按钮调用函数时未捕获类型错误

javascript - 是什么原因导致这个: Uncaught TypeError: $ is not a function?

angular - 返回函数 Angular 2

haskell - 解析 `f = f (<*>) pure`的类型

node.js - npm 错误!名称无效 : "@types/handlebars"

scala - scala.Singleton 是纯编译器小说吗?

python - "TypeError: cannot concatenate ' str ' and ' int ' objects"尝试使用 printf 样式格式时

javascript - 提取一个对象数组内的属性,该对象数组位于另一个对象数组内,并将结果作为字符串数组

javascript - 如何在 Angular 10 中修补动态表单数组的值?