What's New In TypeScript

3.8 & 3.9

Paul Shannon

TypeScript 3.8

Private Fields


						class Person {
							#name: string;

							constructor(first: string, last: string = '') {
								this.#name = first + last;
							}

							get name() {
								return this.#name;
							}
						}
					

Type-only imports and exports


						class InternalClass {
							get type() {
								return 'internal';
							}
						}

						export class MyClass extends InternalClass {}

						export type { InternalClass };
					

Quality of Life

  • export * as something from './something'
  • Top-level await
  • Better change watching

TypeScript 3.9

//@ts-expect-error

Suppresses an error on the following line similar to ts-ignore but produces an error when an error doesn't exist

Fixes

Fixes for Promise

Promise.all and Promise.race were spreading undefined and null types in the result array.

Uncalled function checks

Ternaries now check if functions are used in place of a boolean


						function myFunc() {}

						// TypeScript 3.7 behavior
						if (myFunc) {
							console.log('hello');
						}

						// TypeScript 3.9
						const a = myFunc ? 'a' : 'b';
					

Coming Soon

awaited type

awaited is a new type that correctly unravels Promise<T> types


						function promiseWrap<T>(value: T): Promise<T> {
							return Promise.resolve(value);
						}

						// type Promise<number>
						const value = promiseWrap(1);

						// type Promise<Promise<number>>
						const wrapped = promiseWrap(Promise.resolve(1));

						// type Promise<number>
						const unwrapped = async () => await wrapped;
					

						// written with awaited
						function promiseWrap<T>(value: awaited T): Promise<awaited T> {
							return Promise.resolve(value);
						}

						// type Promise<number>
						const value = promiseWrap(1);

						// type Promise<number>
						const wrapped = promiseWrap(Promise.resolve(1));

						// type number
						const unwrapped = async () => await wrapped;
					

Thanks!