TypeScript 4.0

Return to TypeScript Version History, TypeScript, JavaScript


TypeScript 4.0, released in August 2020, introduced a host of new features, enhancements, and fixes, aimed at improving the developer experience, expanding the language's capabilities, and enhancing type safety. This summary details the major updates provided in this version, along with code examples and comparisons to similar features in other major languages and libraries. Additionally, the necessary links to TypeScript resources are included:

- Language Documentation: [TypeScript Documentation](https://www.typescriptlang.org/docs/) - GitHub Repository: [TypeScript GitHub](https://github.com/microsoft/TypeScript) - Official Website: [TypeScript](https://www.typescriptlang.org/) - Wikipedia: [TypeScript Wikipedia](https://en.wikipedia.org/wiki/TypeScript)

  1. Variadic Tuple Types

TypeScript 4.0 introduced variadic tuple types, allowing developers to type and pass arguments as tuples in a more flexible way, similar to rest parameters in functions.

```typescript function concat<T extends any[], U extends any[]>(first: T, second: U): […T, …U] {

 return [...first, ...second];
} ```

In JavaScript, rest parameters and spread syntax provide similar functionality at runtime without static typing. C# supports params for variadic parameters but lacks a direct equivalent for type-safe concatenation of tuple types. Java's generics and varargs don't offer the same level of type specificity for tuples. Python's typing module supports variadic generics in a somewhat similar manner with `TypeVar` and `Generic`.

  1. Labeled Tuple Elements

TypeScript 4.0 allowed for labeled tuple elements in type annotations, improving the readability of tuple types by allowing developers to provide names for the elements of tuple types.

```typescript type Address = [street: string, city: string, zipCode: number]; ```

While JavaScript doesn't support tuples or type annotations, Python's `NamedTuple` offers a similar capability. C# and Java both have tuple types (ValueTuple in C#, and records in Java since version 14), but the labeling is not as directly integrated into the type system as in TypeScript.

  1. Class Property Inference from Constructors

TypeScript 4.0 can now infer property types in classes from their assignments in the constructor, reducing the need for explicit type annotations.

```typescript class Person {

 name;
 constructor(name: string) {
   this.name = name;
 }
} ```

This feature doesn't have a direct counterpart in JavaScript since it lacks a static type system. C# and Java require explicit type declarations for class properties. Python's type hints, introduced in PEP 484, allow for similar inference in some IDEs, but it's not as integrated into the language.

  1. `unknown` on `catch` Clauses

TypeScript 4.0 introduced the ability to use `unknown` as the type of catch clause variables, encouraging more explicit type-checking inside catch blocks.

```typescript try {

 // some operation
} catch (error: unknown) {
 if (error instanceof Error) {
   console.error(error.stack);
 }
} ```

JavaScript's catch clauses are untyped, reflecting its dynamic nature. C#'s catch blocks can be typed, but default to the base `Exception` type if unspecified. Java's catch blocks must be typed, usually with specific exception classes. Python's exception handling doesn't involve static types, although type hints can be used in a similar manner for documentation.

  1. Editor Improvements

TypeScript 4.0 brought significant improvements to editor support, including faster project loading, better auto-imports, and quicker type-checking, enhancing the developer experience.

While this is specific to TypeScript's tooling and not directly comparable to language features in C#, Java, or Python, it reflects a broader trend towards improving developer productivity through enhanced IDE support across programming languages.

  1. Tuple Types in Rest Parameters and Spread Expressions

Expanding on variadic tuple types, TypeScript 4.0 allows developers to use these types in rest parameters and spread expressions, offering more flexibility in function signatures and calls.

```typescript function invokeArgs<T extends unknown[]>(args: […T]) {

 // ...
} ```

JavaScript supports rest parameters and spread syntax but without TypeScript's type safety. C#'s params and Java's varargs offer similar runtime capabilities but without compile-time type checks. Python's `*args` and `**kwargs` provide dynamic argument passing but lack static typing without type hints.

  1. Custom JSX Factories

TypeScript 4.0 introduced support for custom JSX factories (`jsxFactory` and `jsxFragmentFactory` options), allowing developers to customize how JSX constructs are transformed.

```typescript /** @jsx h */ /** @jsxFrag Fragment */ ```

This feature is specific to the JSX syntax used with frameworks like React and doesn't have direct equivalents in C#, Java, or Python, which do not use JSX.

  1. `–incremental` Build Option Enhancements

The `–incremental` build option in

TypeScript 4.0 saw enhancements, providing faster subsequent builds by leveraging information from previous compilations.

This build optimization technique is specific to TypeScript's compilation process and has parallels in other compiled languages like C# and Java, which have their own mechanisms for incremental compilation to speed up the build process.

  1. Project References with `–noEmit`

TypeScript 4.0 allows project references to be used with the `–noEmit` option, facilitating project setups where type-checking is needed without outputting files.

This feature supports complex project configurations and is somewhat unique to TypeScript's project management capabilities, with no direct equivalents in JavaScript, C#, Java, or Python.

  1. `bigint` Literal Types

TypeScript 4.0 introduced literal types for `bigint`, allowing for more precise typing for large integer values.

```typescript let x: 123n = 123n; ```

JavaScript supports `bigint` literals as part of the language, but without static typing. C#, Java, and Python each have their own ways of representing large integers (`BigInteger` in C# and Java, `int` in Python 3), but none support literal types in the same way as TypeScript.

  1. Mapped Types `as` Clause

The introduction of an `as` clause in mapped types in TypeScript 4.0 provides more flexibility in generating property names in mapped types.

```typescript type MappedTypeWithAs<T> = {

 [P in keyof T as `get${Capitalize}`]: () => T[P]
}; ```

While mapped types are unique to TypeScript's static type system and not present in JavaScript, C#, Java, or Python, the concept of manipulating keys or property names dynamically is possible in these languages through reflection or dynamic typing.

  1. Conclusion

TypeScript 4.0's enhancements significantly improved the language's utility, safety, and developer experience. By introducing features like variadic tuple types, labeled tuple elements, and class property inference, TypeScript continues to offer a powerful type system on top of JavaScript's flexibility. These features, compared to similar ones in languages like C#, Java, and Python, highlight TypeScript's unique approach to solving the challenges of large-scale JavaScript application development. The version's focus on improving both the language's capabilities and the developer's experience ensures that TypeScript remains an essential tool for developers working in the ever-evolving landscape of web development.


TypeScript Version History: TypeScript, JavaScript. TypeScript 4.7 (2022), TypeScript 4.6 (2022), TypeScript 4.5 (2021), TypeScript 4.4 (2021), TypeScript 4.3 (2021), TypeScript 4.2 (2021), TypeScript 4.1 (2020), TypeScript 4.0 (2020), TypeScript 3.9 (2020), TypeScript 3.8 (2020), TypeScript 3.7 (2019), TypeScript 3.6 (2019), TypeScript 3.5 (2019), TypeScript 3.4 (2019), TypeScript 3.3 (2019), TypeScript 3.2 (2018), TypeScript 3.1 (2018), TypeScript 3.0 (2018), TypeScript 2.9 (2018), TypeScript 2.8 (2018), TypeScript 2.7 (2018), TypeScript 2.6 (2017), TypeScript 2.5 (2017), TypeScript 2.4 (2017), TypeScript 2.3 (2017), TypeScript 2.2 (2017), TypeScript 2.1 (2016), TypeScript 2.0 (2016), TypeScript 1.8 (2016), TypeScript 1.7 (2015), TypeScript 1.6 (2015), TypeScript 1.5 (2015), TypeScript 1.4 (2015), TypeScript 1.3 (2014), TypeScript 1.1 (2013), TypeScript 1.0 (2012). (navbar_typecript_versions - see also navbar_typescript, navbar_javacript_versions

TypeScript: TypeScript Glossary, TypeScript Best Practices, Web Development Best Practices, JavaScript Best Practices, TypeScript Fundamentals, TypeScript Inventor - TypeScript Language Designer: Anders Hejlsberg of Microsoft on October 1, 2012; TypeScript Keywords, TypeScript Built-In Data Types, TypeScript Data Structures - TypeScript Algorithms, TypeScript Syntax, TypeScript on Linux, TypeScript on macOS, TypeScript on Windows, TypeScript on Android, TypeScript on iOS, TypeScript Installation, TypeScript Containerization (TypeScript with Docker, TypeScript with Podman, TypeScript and Kubernetes), TypeScript OOP - TypeScript Design Patterns, Clean TypeScript - TypeScript Style Guide, TypeScript Best Practices - TypeScript BDD, Web Browser, Web Development, HTML-CSS, TypeScript Frameworks (Angular), JavaScript Libraries (React.js with TypeScript, Vue.js with TypeScript, jQuery with TypeScript), TypeScript on the Server (TypeScript with Node.js, TypeScript with Deno, TypeScript with Express.js), TypeScript Compiler (tsc, tsconfig.json), TypeScript Transpiler (Transpile TypeScript into JavaScript), Babel and TypeScript, TypeScript Package Management, NPM and TypeScript, NVM and TypeScript, Yarn Package Manager and TypeScript, TypeScript IDEs (Visual Studio Code, Visual Studio, JetBrains WebStorm), TypeScript Development Tools, TypeScript Linter, TypeScript Debugging (Chrome DevTools, JavaScript Source Maps), TypeScript Testing (TypeScript TDD, Selenium, Jest, Mocha.js, Jasmine, Tape Testing (tap-producing test harness for Node.js and browsers), Supertest, React Testing Library, Enzyme.js React Testing, Angular TestBed), TypeScript DevOps - TypeScript SRE, TypeScript Data Science - TypeScript DataOps, TypeScript Machine Learning, TypeScript Deep Learning, Functional TypeScript, TypeScript Concurrency (WebAssembly - WASM) - TypeScript Async (TypeScript Await, TypeScript Promises, TypeScript Workers - Web Workers, Service Workers, Browser Main Thread), TypeScript Concurrency, TypeScript History, TypeScript Bibliography, Manning TypeScript Series, TypeScript Glossary, TypeScript T, TypeScript Courses, TypeScript Standard Library, TypeScript Libraries, TypeScript Frameworks (Angular), TypeScript Research, JavaScript, TypeScript GitHub, Written in TypeScript, TypeScript Popularity, TypeScript Awesome, TypeScript Versions. (navbar_typescript - see also navbar_javascript, navbar_javascript_libraries, navbar_typescript_libraries, navbar_typescript_versions, navbar_typescript_standard_library, navbar_typescript_libraries, navbar_typescript_reserved_words, navbar_typescript_functional, navbar_typescript_concurrency, navbar_typescript_async, navbar_javascript_standard_library, navbar_react.js, navbar_angular, navbar_vue, navbar_javascript_standard_library, navbar_web_development)


© 1994 - 2024 Cloud Monk Losang Jinpa or Fair Use. Disclaimers

SYI LU SENG E MU CHYWE YE. NAN. WEI LA YE. WEI LA YE. SA WA HE.