TypeScript 4.3 Released: New Features Including Separate Getter/Setter Types, Override Keyword, Template Literal Improvements, and Private Fields
TypeScript 4.3 introduces several enhancements such as distinct types for read/write properties, the override keyword with a no‑implicit‑override flag, richer template literal type inference, and expanded support for truly private class members, all illustrated with code examples.
TypeScript 4.3 has been released. This version adds many new features, such as support for specifying separate types for read and write properties, the "override" and "--noImplicitOverride" flags, improvements to template literal types, and more.
The specific updates are as follows:
Support for specifying separate types for read and write properties. However, the getter type must be assignable to the setter type. In other words, the getter’s type must be assignable to the setter, ensuring a degree of consistency so that a property can always be assigned to itself. class Thing { #size = 0; get size() { return this.#size; } set size(value) { let num = Number(value); // Don't allow NaN and stuff. if (!Number.isFinite(num)) { this.#size = 0; return; } this.#size = num; } }
Added the "override" keyword. When a method is marked with "override", TypeScript ensures that a method with the same name exists in the base class. Also introduced the "--noImplicitOverride" flag, which makes any overriding of base class methods an error unless the "override" keyword is explicitly used. class SomeComponent { setVisible(value: boolean) { // ... } someHelperMethod() { // ... } } class SpecializedComponent extends SomeComponent { override setVisible(value: boolean) { // ... } override show() { // ~~~~~~~~ // Error! This method can't be marked with 'override' because it's not declared in // 'SomeComponent'. // ... } // Oops! We weren't trying to override here, // we just needed to write a local helper method. someHelperMethod() { // ... } // ... }
Improvements to template literal types. When TypeScript sees a template literal passed to an expression that requires a literal type, it attempts to give that expression a template type. Additionally, TypeScript can now better correlate and infer between different template literal types. function bar(s: string): `hello ${string}` { // Previously an error, now works! return `hello ${s}`; } declare let s1: `${number}-${number}-${number}`; declare let s2: `1-2-3`; declare let s3: `${number}-2-3`; declare let s4: `1-${number}-3`; declare let s5: `1-2-${number}`; declare let s6: `${number}-2-${number}`; // Now *all of these* work! s1 = s2; s1 = s3; s1 = s4; s1 = s5; s1 = s6;
TypeScript 4.3 extends which class elements can be given a #private name, making them truly private at runtime. In addition to properties, methods and accessors can also be given private names.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.