TypeScript 4.1 released, new template literal type

The TypeScript team released TypeScript 4.1, which includes powerful template literal types, key remapping for mapping types, and recursive condition types.

The template literal type has received a very enthusiastic response in the community. This new feature provides the ability to use ordinary string literal types as other type definitions, which makes it easy to create and execute template syntax.

The template string literal can also be dynamically generated and inferred based on the replacement position in the template string. To further support template string literals, TypeScript has also added new practical type aliases to modify letter case: Uppercase, Lowercase, Capitalize, and Uncapitalize.

The community provides many interesting template string text examples, including querySelector, router parameter parsing, expression parsing, JSON parsing and serialization, GraphQL type AST, SQL query verification, CSS parsing, games, spell checking, etc.

Haskell and PureScript have similar features, and TypeScript now supports them.

TypeScript 4.1 also improves the mapping type by adding key remapping. The mapping type was previously limited to new object types with known builds, and now supports creating new keys or filtering existing keys.

Another important new feature of TypeScript 4.1 is the recursive conditional type, which can more easily support flat methods for arrays or complex promise trees. Conditional types can now refer to themselves in branches immediately, making it easier to create recursive type aliases. The TypeScript team warns that this mode should be used with caution to avoid slowing down recursive type checking, and if the supported recursion depth is exceeded, the TypeScript compiler will throw a compile-time error.

TypeScript 4.1 adds a new compiler flag --noUncheckedIndexedAccess to identify potential unchecked attributes. This new feature will not be automatically included in the --strict flag, because it will change behavior in some common scenarios, such as when traversing the index of a for loop.

There are two new JSX options for React 17 users that can better support production and development compilation, react-jsx and react-jsxdev.

When upgrading to TypeScript 4.1, you need to consider the following major changes:

  • The behavior of the built-in lib.d.ts to automatically generate DOM types has changed, and Reflect.enumerateAPI has been removed because ES2016 has deprecated it.

  • The abstract member of the class is no longer marked as async. The caller only cares about the return type, so there is no longer a value that specifies the abstract member as async.

  • The any and unknown types are now propagated in the wrong place.

  • The resolve parameter is now required in promises. TypeScript 4.1 includes a quick fix to simplify the upgrade process.

  • Conditional expansion can create optional attributes.

  • Parameters that do not match are no longer relevant.

Work on TypeScript 4.2 is already in progress and is expected to be completed in February 2021. TypeScript 4.2 includes generalized index signatures, the first/middle remaining elements in tuple types, --noImplicitOverride, --noPropertyAccessFromIndexSignature, static index signatures, typeof class, faster compile-time iteration, and further improvements in editors and productivity Improve.

TypeScript uses the Apache 2 open source license. Developers are welcome to participate in contributions and feedback through the TypeScript GitHub project, and follow the TypeScript contribution guidelines and Microsoft's open source code of conduct.

Guess you like

Origin blog.csdn.net/lianjiuxiao/article/details/113357878