Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

typescript - Optional chaining operator support in VSCode

React Native 0.56 provides native support of Optional Chaining Operator i.e ?.

However the latest stable release of VS Code can not recognize this syntax and throw a TypeScript validation error:

[ts] Expression expected.

while no complie-time or eslint error was thrown.

How can I do to fix it?


Update on 2019/12/11:

Optional chaining is offically supported by TypeScript since 3.7! ??

If you still meet this error, probably the TypeScript lib shipped with your VSCode is < 3.7.

You can fix it simply by installing the latest version of typescript (>= 3.7) to your workspace. (by npm or yarn as dev deps)

Then open the command palette, type tstv, and switch the TypeScript version used for JS and TS features from VS Code's Version to Workspace's Version (make sure it's >= 3.7) and voila!

This is definitely an awesome feature, thanks your all and happy hacking!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

VS Code 1.41 supports optional chaining in both JavaScript and TypeScript files. This support includes syntax highlighting and IntelliSense.

If you are using VS Code 1.41+ and optional chaining is not working as expected, try:

  • Check your installed extensions. Some of them may not yet understand optional chaining, which could cause errors or bad syntax highlighting

  • If you are using a workspace typescript version, make sure it is TypeScript 3.7+


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...