TypeScript 3.8 Adds Private Fields and Top-Level Await

栏目: IT技术 · 发布时间: 4年前

内容简介:The TypeScript team announced theWhile the importing and exporting of types would often work because of import elision, ambiguity occurred when it was unclear if it was a type or value getting imported. Import statements that only contained imports used as

The TypeScript team announced the release of TypeScript 3.8 , which includes type-only imports and exports, private fields, and top-level await.

While the importing and exporting of types would often work because of import elision, ambiguity occurred when it was unclear if it was a type or value getting imported. Import statements that only contained imports used as types would get eliminated by TypeScript's import elision. To work around these issues, TypeScript 3.8 adds a new syntax for importing and exporting types:

import type { SomeThing } from "./some-module.js";
export type { SomeThing };

The import type statement only imports declarations to use for type annotations and declarations and always gets fully erased during transpilation. The export type command only provides an export for type contexts, also getting erased from TypeScript’s output. When using import type to import a class, the class cannot get extended.

The TypeScript compiler introduces a new flag, importsNotUsedAsValues , to determine what happens to imports not used at run-time. The values for this flag,  remove , preserve , or  error, provide developers with more control over importing and exporting types.

The ECMAScript private fields proposal recently reached stage 3 due to significant efforts from Bloomberg and the TypeScript team.

Private fields start with a # character and get uniquely scoped to their containing class. TypeScript modifiers like public or private cannot get used on private fields. Private fields cannot get accessed or detected outside of the containing class.

Private fields are different than the TypeScript private keyword. As explained by Daniel Rosenwasser, program manager of the TypeScript team:

When it comes to properties, TypeScript’s private modifiers are fully erased – that means that while the data will be there, nothing is encoded in your JavaScript output about how the property was declared. At runtime, it acts entirely like a normal property. That means that when using the private keyword, privacy is only enforced at compile-time/design-time, and for JavaScript consumers, it’s entirely intent-based. The upside is that this sort of “soft privacy” can help your consumers temporarily work around not having access to some API, and works in any runtime. On the other hand, ECMAScript’s # privates are completely inaccessible outside of the class.

Note that TypeScript currently only supports private fields transpilation to ES6 and higher as the backward-compatible implementation leverages WeakMaps, whereas the private keyword works back to ES3.

ECMAScript 2020 adds export * syntax to expose all members of a module as a single member. Previously developers would need to use this syntax:

import * as utilities from "./utilities.js";
export { utilities };

With ECMAScript 2020 and TypeScript 3.8, this gets reduced to:

export * as utilities from "./utilities.js";

The introduction of promises in ES6 provided the foundation for better asynchronous handing in JavaScript, but has led to many additional features in the five years since its initial release, such as async/await. The latest addition is top-level await, which allows await at the top level of a module.

Top-level await is now also supported by TypeScript 3.7, though only can get transpiled to ES2017 and newer environments, and the modules support is esnext or system , as it requires an environment with ES module support.

TypeScript 3.8 now supports es2020 as a transpilation target and module option. This mode preserves the ES2020 features added inTypeScript 3.7 and 3.8.

The TypeScript 3.8 release improves its directory watching support and adds a new watchOptions compiler configuration field. These options give developers greater control over how directories and files get watched, improving performance and reliability in working with changes to node_modules .

The watchOptions field provides four new options: watchFile , watchDirectory , fallbackPolling , and synchronousWatchDirectory , each of which has several configuration options for how often to check files and directories.

The TypeScript 3.8 compiler also includes a new compiler performance optimization option, assumeChangesOnlyAffectDirectDependencies , which tells TypeScript only to recheck and rebuild files that have changed as well as files that directly import them.

With each TypeScript release, the compiler introduces breaking changes as its type checking capabilities improves. In TypeScript 3.8, the compiler adds stricter assignability checks to unions with index signatures.

In the TypeScript community, after two TSConf events in Seattle in 2018 and 2019, the first TSConf Japan and TSConf.eu events are occurring in February and March.

TypeScript is open source software available under the Apache 2 license. Contributions and feedback are encouraged via the TypeScript GitHub project and should follow the TypeScript contribution guidelines and Microsoft open-source code of conduct .


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

游戏运营:高手进阶之路

游戏运营:高手进阶之路

饭大官人 / 电子工业出版社 / 2018-1-1 / 79.00元

《游戏运营:高手进阶之路》是一本系统的、成体系的、注重运营效能、强化系统思维、提升专业认知的书籍。《游戏运营:高手进阶之路》几乎完整覆盖了一个游戏运营人员日常工作中的方方面面,并从工作中具体的业务场景出发,归纳整理出各种解决问题的方法论。《游戏运营:高手进阶之路》为广大游戏从业者建立了完整的知识技能成长体系,包含两大岗位基本功—内容输出和协作推进,四大职业技能—活动策划、版本管理、用户运营、数据分......一起来看看 《游戏运营:高手进阶之路》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

SHA 加密
SHA 加密

SHA 加密工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具