gpt4 book ai didi

Typescript 可选链接错误 : Expression expected. ts(1109)

转载 作者:行者123 更新时间:2023-12-04 01:12:22 29 4
gpt4 key购买 nike

我正在尝试在 Typescript + React Native 中进行可选链接。

假设我有以下接口(interface):

interface Bar {
y: number
}

interface Foo {
x?: Bar
}

我尝试运行以下命令:
 const test: Foo = {x: {y: 3}};
console.log(test.x?.y);

VSCode 会在 ?. 下显示错误说以下内容:Expression expected.ts(1109)

你知道为什么会发生这种情况或我应该如何解决它?谢谢。

最佳答案

TypeScript 3.7 已经发布,但稳定的 VS Code 使用的是旧版本。

尝试 ⌘ + Shift + p 并选择 Select TypeScript Version .如果不是 3.7+,那就是问题所在。最简单的解决方法是安装 ms-vscode.vscode-typescript-next延期。它将提供一个夜间 TypeScript 版本供 VS Code 使用(可能需要重新启动 VS Code,仅供引用)。

当 VS Code 默认获得 TypeScript 3.7+ 时,您需要记住删除扩展。

https://code.visualstudio.com/docs/typescript/typescript-compiling#_using-newer-typescript-versions更多细节。

关于Typescript 可选链接错误 : Expression expected. ts(1109),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54611471/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com