Relaxed Rules Between Optional Properties and String Index Signatures

TypeScript 5.4 relaxes the rules governing the interaction between optional properties and string index signatures, providing more flexibility in type definitions.

Syntax:

interface MyObj {
[key: string]: number;
optionalProp?: string;
}

Example: Defining an Object with Optional Property in this example, we define an interface MyObj with a string index signature and an optional property.

JavaScript
interface MyObj {
    [key: string]: number;
    optionalProp?: string;
}

const obj: MyObj = {
    optionalProp: 'gfg',
    key: 22,
};

console.log(obj.optionalProp);
console.log(obj['key']);

Output

gfg
22


What’s new in TypeScript 5.4 ?

TypeScript 5.4 brings forth several new capabilities and refinements, aiming to enhance developer productivity and code quality. Some of the new capabilities that are added or Introduced in TypeScript 5.4 are as follows:

Table of Content

  • Variadic Tuple Types
  • Class Property Inference from Constructors
  • Local Assignment Discouragement
  • Relaxed Rules Between Optional Properties and String Index Signatures

Similar Reads

Variadic Tuple Types

Variadic tuple types allow developers to define tuple types with a variable number of elements, enhancing flexibility and type safety....

Class Property Inference from Constructors

TypeScript 5.4 allows class properties to be inferred directly from constructor arguments, reducing verbosity and improving code readability....

Local Assignment Discouragement

TypeScript 5.4 introduces stricter checks to discourage accidental local assignments within conditions or loops, enhancing code quality and preventing potential bugs....

Relaxed Rules Between Optional Properties and String Index Signatures

TypeScript 5.4 relaxes the rules governing the interaction between optional properties and string index signatures, providing more flexibility in type definitions....